App.get_running_app().root.my_method()-'NoneType'对象没有属性'my_method() [英] App.get_running_app().root.my_method() - 'NoneType' object has no attribute 'my_method()

查看:137
本文介绍了App.get_running_app().root.my_method()-'NoneType'对象没有属性'my_method()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在按下按钮后在Screenmanager上调用一个函数.但是对(App.get_running_app().root.)的调用没有给我一个对象.

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.

似乎没有一个根对象,但是为什么会这样.在我尝试使用仪表板之前,它就奏效了.

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.

我尝试包括ScreenManager类中无法调用的方法 并从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文件:

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文件:

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

错误消息 AttributeError:'NoneType'对象没有属性 'get_character_selection_screen'

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

缩进可能被弄乱了,因为我试图将其上传到这里.

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

推荐答案

尝试使用时钟对其进行计划,因此确保应用程序和小部件已准备就绪

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

这篇关于App.get_running_app().root.my_method()-'NoneType'对象没有属性'my_method()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆