python-3.xkivykivy-languagekivymd

How to get identifier declarative style kivy


I have this code

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.widget import Widget
from kivymd.app import MDApp
from kivymd.uix.button import MDButton, MDButtonText
from kivymd.uix.dialog import MDDialog, MDDialogHeadlineText, MDDialogSupportingText, MDDialogButtonContainer, \
    MDDialogContentContainer
from kivymd.uix.textfield import MDTextField, MDTextFieldLeadingIcon, MDTextFieldHelperText

KV = '''
ScreenManager:
    Screen_1:
        id: screen_1
        name: 'screen_1'


<Screen_1>
    MDScreen:
        id: screen_1
        BoxLayout:
            Button:
                text: 'open dialog'
                pos_hint: {'center_x': .5, 'center_y': .5}
                on_press:
                    root.open_dialog()

'''


class Test(MDApp):

    def build(self):
        return Builder.load_string(KV)


class Screen_1(Screen):

    def open_dialog(self):
        self.search_dialog = MDDialog(
            MDDialogHeadlineText(text='Find human'),
            MDDialogSupportingText(text="Input name"),
            MDDialogContentContainer(
                MDTextField(
                    MDTextFieldLeadingIcon(icon='magnify'),
                    MDTextFieldHelperText(text="Helper text"),
                    id='search_name',
                    mode="outlined"),
            ),
            MDDialogButtonContainer(
                Widget(),
                MDButton(
                    MDButtonText(text="Find"),
                    style="text",
                    on_release=self.dialog_search_button_click()
                ),
                spacing="8dp",
            ), id='dialog'
        )
        self.search_dialog.open()

    def dialog_search_button_click(self):
        dialog = MDDialog
        print(dialog.ids['content_container'].children[0].text)

Test().run()

I need to get text from inpur field in the MDDialog, but I can't understand how to get it. I know that the declarative style is no longer supported in kivy, but there is no other option in kivymd. I'll try both self and self.root, but it doesn't work

KivyMD: 2.0.1dev


Solution

  • Try saving a reference to the MDTextField. In your open_dialog() method, create the MDTextField in a separate line:

                self.mdtf = MDTextField(
                    MDTextFieldLeadingIcon(icon='magnify'),
                    MDTextFieldHelperText(text="Helper text"),
                    mode="outlined")
    

    Then use that in both the open_dialog() method:

        self.search_dialog = MDDialog(
            MDDialogHeadlineText(text='Find human'),
            MDDialogSupportingText(text="Input name"),
            MDDialogContentContainer(
                self.mdtf
            ),
            MDDialogButtonContainer(
                Widget(),
                MDButton(
                    MDButtonText(text="Find"),
                    style="text",
                    on_release=self.dialog_search_button_click()
                ),
                spacing="8dp",
            )
        )
    

    and in the dialog_search_button_click() method:

    def dialog_search_button_click(self):
        print(self.mdtf.text)
    

    Note that defining an id in python code will not work, it must be defined in kv.