Search code examples
pythonkivydashboard

App.get_running_app().root.my_method() - 'NoneType' object has no attribute 'my_method()


I try to call a function on my Screenmanager after a button was pressed. But the call to (App.get_running_app().root.) does not get me an object.

The Buttons will not work for me and i do not know why.

There seems to be an issue that i do not have a root object, but why. It worked before i tried it with the dashboard.

I have tried including methods from the ScreenManager Class which i could not call and call methods out of the Dashboard class, which function.

python file:

from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
class DashboardScreen(Screen):

    def __init__(self, **kwargs):
        super(DashboardScreen, self).__init__(**kwargs)
        # Initialize Target Container
        App.get_running_app().root.get_character_selection_screen()


class MyScreenManager(ScreenManager):

    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        self.add_widget(DashboardScreen(name='dashboard'))

    def get_character_selection_screen(self):
        pass


class MatrixApp(App):

    def build(self):
        return MyScreenManager()


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

Kivy File:

<DashboardScreen>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "test"

Error Message AttributeError: 'NoneType' object has no attribute 'get_character_selection_screen'

Indentation might be messed up, because i tried to upload it here.


Solution

  • Try to schedule it with clock, so you are sure the app and widgets are ready

    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.app import App
    from kivy.clock import Clock
    
    class DashboardScreen(Screen):
    
        def __init__(self, **kwargs):
            super(DashboardScreen, self).__init__(**kwargs)
            Clock.schedule_once(self.after_init)
    
        def after_init(self, dt):
            App.get_running_app().root.get_character_selection_screen()