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
Try saving a reference to the
MDTextField. In youropen_dialog()method, create theMDTextFieldin a separate line:Then use that in both the
open_dialog()method:and in the
dialog_search_button_click()method:Note that defining an
idin python code will not work, it must be defined inkv.