'Kivymd Custom Input Dialog. problem with getting text

I am creating an Input Dialog using kivymd. Whenever I try to fetch the text from the text field, it doesn't output the text, rather it seems like the text is not there. (the dialog just pops up ok and the buttons are working fine).

part of the kivy code

<Content>
    MDTextField:
        id: pin
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        color_mode: 'custom'
        line_color_focus: [0,0,1,1]

part of the python code

class Content(FloatLayout):
    pass

class MenuScreen(Screen):
    def __init__(self, **kwargs):
        super(MenuScreen, self).__init__(**kwargs)

    def show_confirmation_dialog(self):
        # if not self.dialog:
        self.dialog = MDDialog(
            title="Enter Pin",
            type="custom",
            content_cls=Content(),
            buttons=[
                MDFlatButton(
                    text="cancel",on_release=self.callback
                ),
                MDRaisedButton(
                    text="[b]ok[/b]",
                    on_release=self.ok,
                    markup=True,

                ),
            ],
            size_hint_x=0.7,
            auto_dismiss=False,

        )
        self.dialog.open()

    def callback(self, *args):
        self.dialog.dismiss()

    def ok(self, *args):
        pin = Content().ids.pin.text

        if pin == "":
            toast("enter pin")

        else:
            toast(f"pin is {pin}")


Solution 1:[1]

You can call the Content class to a variable and use it in the MDDialog.content_cls.

def show_custom_dialog(self):
    content_cls = Content() # call the class to a variable
    self.cdialog = MDDialog(content_cls=content_cls, 
           type='custom', title='Enter Pin'
            )
    self.cdialog.buttons = [
            MDFlatButton(text='cancel',on_release=self.close_dialog),
            MDRaisedButton(text='Ok',
            on_release=lambda x:self.get_data(x,content_cls))
            ]
    self.cdialog.open()

Then create a function that will get the event_button and the content class as arguments by use of lambda expression in the button as shown above.

def get_data(self,instance_btn, content_cls):
    textfield = content_cls.ids.pin
    # get input
    value = textfield._get_text()
    # do stufs here
    toast(value)

At this point you can extract any id in the Content class.I hope this helps. Refer below for full code

from kivmd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton, MDRaisedButtom
from kivy.lang.builder impor Builder
from kivy.toast import toast


kv = '''
MDBoxLayout:
    orientation : 'vertical'
    
    MDFlatbutton:
        text : 'Dialog'
        pos_hint : {'center_x':.5'}
        on_release : app.show_custom_dialog()

<Content>:
    MDTextField:
        id : pin
        pos_hint : {'center_x':.5,'center_y':.5}

'''

class Content(MDFloatLayout):
    pass


class InputDialogApp(MDApp):
    cdialog = None

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

    def show_custom_dialog(self):
        content_cls = Content()
        self.cdialog = MDDialog(title='Enter Pin',
                 content_cls=content_cls,
                type='custom')
        self.cdialog.buttons = [
    
                MDFlatButton(text="Cancel",on_release=self.close_dialog), 

               MDRaisedButton(text="Ok",on_release=lambda x:self.get_data(x,content_cls))
                ]
        self.cdialog.open()

    def close_dialog(self, instance):
        if self.cdialog:
            self.cdialog.dismiss()

    def get_data(self, instance_btn, content_cls):
        textfield = content_cls.ids.pin
        value = textfield._get_text()
        # do stuffs here
        toast(value)
    
        self.close_dialog(instance_btn)

if __name__ == '__main__':
    InputDialogApp().run()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1