Unable to access Widget ids in Python

Viewed 54

Code:

from kivymd.app import MDApp
from kivy.properties import ObjectProperty
from kivy.lang import Builder
KV = """
MDScreen:
    submit_btn: submit_btn
    MDRaisedButton:
        id: submit_btn
        text:'Submit'
        on_press: app.btn()
"""
class Test(MDApp):
    submit_btn = ObjectProperty(None)
    def build(self):
        return Builder.load_string(KV)
    def btn(self):
        print(self.submit_btn.state)

Test().run()

On pressing the button, I receive the following error:

AttributeError: 'NoneType' object has no attribute 'state'

I am unable to access id of widgets. What am I doing wrong?

1 Answers

This is pretty similar to your previous question a few minutes ago: you have not set self.submit_btn to anything except None. It looks like you tried to in the kv rule, but you've actually set the property of the MdScreen instance which is your root widget, accessed as self.root.submit_btn.

Related