Search code examples
pythonkivykivy-languagekivymd

Retrieving the App.get_running_app().root.ids returns an empty dictionary (Can't retrieve the ids)


I am trying to retrieve the ids of the widgets under the MDApp, however I keep getting an empty dictionary printed out. I found that the ids dict wasn't populated after debugging a problem where whenever I try to access any other widget using it's ID I would get an error.

At first, I tried printing the ids out straight in the build function under MDApp, then in the on_start function, and finally tried printing it out by pressing a button:

PS: I tried also printing out the id of the button itself which was defined as ( """ MenuButton: id: Menu_Button "" ) however, that too came back as empty (an empty string space is printed out).


class MenuButton(MDFloatingActionButton):
    def press(self):
        app = MDApp.get_running_app()
        print(app.root.ids)
        print(self.id)


Solution

  • The ids dictionary is not part of the App instance, they are added to the root widget under which they are defined. See the documentation. The documentation states:

    ids are added to the root widget’s ids dictionary

    The root widget is the widget in which the id is defined in the kv, not the root widget of the App Poor choice of wording by the documenters. The ids would only appear in the App.root if the ids are defined in a kv definition for a widget that is actually the App.root.