Python/KivyMD: How to print the text from a MDTextField that is inside an MDScreen in python/kivymd

898 views Asked by At

Hi i'm fairly new to kivymd and i'm trying to print the text that a user inserts in a MDTextField that is inside a screen when pressing a button. Normally I'd use self.screen.ids.name_of_id.text to get access to the text from the .py file however that doesn't seem to work when using screens and gives the error AttributeError: 'super' object has no attribute 'getattr'. Is there a way to get access to the text from the .py file?

my .kv snippet

ScreenManager:
    ForgotPassword:
    Login:
    ResetPassword:
    Main:

<ForgotPassword@Screen>
    name: "forgot"
    MDFloatLayout:

        MDTextField:
            id: forgot_email
            hint_text: "Email"
            size_hint: .7, None
            pos_hint: {"center_x": .5}

        MDFillRoundFlatButton:
            text: "                Requesitar Nova Password                "
            md_bg_color: 0, 0.4, 1, 1
            text_color: 1, 1, 1, 1
            font_size: 16
            pos_hint: {"center_x": .5, "center_y": .15}
            on_press: app.forgot_password()

my .py code

class MainApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Blue"
        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_hue = "500"
        self.screen = Builder.load_file("Backup.kv")
        return self.screen

    def forgot_password(self):
        print(self.screen.ids.forgot_email.text)



MainApp().run()
1

There are 1 answers

0
John Anderson On BEST ANSWER

The self.screen in your print() statement is the ScreenManager, which, in your case, will have no ids, causing the error. The ids are only available in the root of each rule in the kv. So, in your kv, only ScreenManager and ForgotPassword can have ids. But no ids are defined in the ScreenManager, so it will have no ids. The forgot_email is defined under the ForgotPassword rule, so that id will appear in the ids of the ForgotPassword Screen instance. So, to access that MDTextField, try:

def forgot_password(self):
    print(self.screen.get_screen('forgot').ids.forgot_email.text)