【问题标题】:User text not showing after trying to access it in MDTextField (KivyMD)尝试在 MDTextField (KivyMD) 中访问后未显示用户文本
【发布时间】:2021-05-03 14:29:42
【问题描述】:

使用 KivyMD 在 MDDialog 中创建 MDTextField 后,我试图访问用户在 MDTextField 中输入的文本,并将标签的文本更改为输入的任何内容,但无济于事。下面是我的代码:

# importing all the necessary modules and inheriting my dialog from boxlayout

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout   
from kivymd.uix.dialog import MDDialog     

# creating a string which will contain all my kv definitions that I would put in a .kv file

the_kv_file = '''
<ContentOfAppointmentDialog>:
    orientation: "vertical"
    spacing: "10dp"
    size_hint: None, None
    size: "250dp", "150dp"

    MDTextField:
        hint_text: "Enter details"
        id: the_textfield
        helper_text: "Be detailed and precise"
        helper_text_mode: "on_focus"
        max_text_length: 75
        multiline: True
        mode: "rectangle"

    MDFlatButton:
        text: "DISCARD"
        on_release: app.CloseDialog()
    MDFlatButton:
        text: "DONE"
        on_release: app.ChangingLabel()

MDFloatLayout:
   MDLabel:
       id: label_to_change
       text: "hello"
   MDFloatingActionButton:
       icon: 'pencil'
       on_press: app.OpenDialog()  
'''
# Creating the classes of My main app and the dialog as well as the required functions

class ContentOfAppointmentDialog(BoxLayout):    # this defines the dialogs layout
    pass


class MainApp(MDApp):   # my main app
    def build(self):
        self.main_screen = Builder.load_string(the_kv_file)
        self.dialog = MDDialog(title="my example", type="custom", content_cls=ContentOfAppointmentDialog()) # defining the dialog
        self.AccessDialog = ContentOfAppointmentDialog()

        return self.main_screen

    def OpenDialog(self):   # opens dialog
        self.dialog.open()


    def CloseDialog(self):  # closes dialog
        self.dialog.dismiss()

# this is the function that is not working as I intended it to    
    def ChangingLabel(self):    # function that is supposed to change the label to what users input is
        self.main_screen.ids.label_to_change = self.AccessDialog.ids.the_textfield.text
        self.dialog.dismiss()


MainApp().run()

【问题讨论】:

    标签: python textfield kivymd


    【解决方案1】:

    您可以使用我的示例代码,我在您提供的代码中更改了一些内容,您始终可以通过尝试将它们与应用主类中定义的函数链接起来,从任何地方访问内容或小部件,始终遵循来自 Kivy Lang、jbsidis 的 Root 访问权限:

    from kivymd.app import MDApp
    from kivy.lang import Builder
    from kivy.uix.boxlayout import BoxLayout   
    from kivymd.uix.dialog import MDDialog     
    
    always_use_a_single_python_script_that_includes_everything_including_the_kv_lang_section = '''
    <ContentOfAppointmentDialog>:
        orientation: "vertical"
        spacing: "10dp"
        BoxLayout:
            orientation: "vertical"
    
            MDTextField:
                id: the_textfield
                hint_text: "Enter details"
                helper_text: "Be detailed and precise"
                helper_text_mode: "on_focus"
                max_text_length: 75
                multiline: True
                mode: "rectangle" #if you need to change text in real time while typing, you have to use the on_text: event and link it like the Done button function, on_text: app.ChangingLabel(root.ids.the_textfield.text) or also on_text: app.ChangingLabel(self.text)
            BoxLayout: #jbsidis #this is needed to have the buttons Horizontally
                MDFlatButton:
                    text: "Discard"
                    on_release:
                        app.CloseDialog()
                MDFlatButton:
                    markup: True
                    text: "[b]Done!"
                    on_release:
                        app.ChangingLabel(root.ids.the_textfield.text)
    
    MDFloatLayout:
    
        #BoxLayout:
        MDTextButton:
            markup: True
            pos_hint: {"center_x": .5, "center_y": .7}
            id: label_to_change
            text: "[color=000000]jbsidis"
        MDFloatingActionButton:
            pos_hint: {"center_x": .5, "center_y": .5}
            icon: 'pencil'
            on_press: app.OpenDialog()  
    '''
    # Creating the classes of My main app and the dialog as well as the required functions
    class ContentOfAppointmentDialog(BoxLayout):    # this defines the dialogs layout
        pass
    #jbsidis
    class MainApp(MDApp):   # my main app
        def build(self):
            self.main_screen = Builder.load_string(always_use_a_single_python_script_that_includes_everything_including_the_kv_lang_section)
            self.dialog = MDDialog(auto_dismiss=False,title="Enter your name:", type="custom", content_cls=ContentOfAppointmentDialog()) # defining the dialog
            #self.AccessDialog = ContentOfAppointmentDialog() #this is a great idea you just need to change a few things
            return self.main_screen
        def OpenDialog(self):   # opens dialog
            self.dialog.open()
        def CloseDialog(self):  # closes dialog
            self.dialog.dismiss()
    
    # this is the function that is not working as I intended it to    
        def ChangingLabel(self,this_argument_helps_to_transport_the_text_entered_in_the_text_field_of_the_dialog):    # function that is supposed to change the label to what users input is
            #self.main_screen.ids.label_to_change = self.AccessDialog.ids.the_textfield.text
            #ROOT.ids.AnyID_in_the_root_of_the_kv_lang
            self.main_screen.ids.label_to_change.text="[b][color=ff0000]"+str(this_argument_helps_to_transport_the_text_entered_in_the_text_field_of_the_dialog)
            self.dialog.dismiss()
    
    
    MainApp().run()
    

    您会发现,在许多情况下,您将不得不定义许多带有参数或非必需参数的函数或类,但它们是使应用程序正常工作所必需的,这是编程需要的,来自萨尔瓦多的问候,图片:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2023-01-24
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 2020-11-30
      相关资源
      最近更新 更多