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

跟風遠走 提交于 2019-12-11 15:05:26

问题


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.


回答1:


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()


来源:https://stackoverflow.com/questions/56245754/app-get-running-app-root-my-method-nonetype-object-has-no-attribute-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!