简体   繁体   English

kivy,无法访问app.root属性

[英]kivy, cant access app.root property

I have the following codes in my main.py and spend.kv as follows 我在main.py和支出.kv中有以下代码,如下所示

main.py main.py

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class Manager(ScreenManager):
   currency = '$'

class SpendApp(App):
   def build(self):
      control = Manager()
      return control

class First(Screen):
   pass

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

spend.kv 花.kv

<Manager>:
    First


<First>:
    GridLayout:
        cols: 1
        Label: 
            text: 'Total spending'
            height: '48dp'
            size_hint_y: None
         Amount:
            height: '38dp'
            size_hint_y: None
            font_color: 1,0,0,1



<Amount@Label>:
   text: app.root.currency + '0.0'

when i run this the program crashes with the following error: 当我运行此程序时,程序崩溃并显示以下错误:

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

I know this is because of my referance to app.root.currency in my spend.kv file: 我知道这是因为我在我的flower.kv文件中引用了app.root.currency:

<Amount@Label>:
   text: app.root.currency + '0.0'

so is there a way to make this reference correctly, without receiving this error? 因此,有没有一种方法可以正确地进行此引用,而不会收到此错误?

If you use following code: 如果使用以下代码:

<Amount@Label>:
    text: str(root) # 

you'll discover that the root of Amount object is the Amount object itself because when you're defining it it's not in any hierarchy yet. 您会发现Amount对象的根是Amount对象本身,因为在定义它时,它不在任何层次结构中。 You can access root widged only inside actual hierarchy: 您只能在实际层次结构中访问root widd:

<First>:
    GridLayout:
        cols: 1
        Label: 
            text: str(root)

In this hierarchy, root object is definied as a object of First class, which is actually an instance of Screen widget, so you have to use manager attribute in order to acces your Manager class: 在此层次结构中, root对象被定义为First类的对象,它实际上是Screen小部件的一个实例,因此必须使用manager属性才能访问Manager类:

<First>:
    GridLayout:
        cols: 1
        Label: 
            text: root.manager.currency  + '0.0'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法访问我的 kivy 类中的对象,不能使用“app.root”和“self.ids” - Cant access the objects in my kivy class, can't use 'app.root' and ‘self.ids’ Kivy:App.root 中的无效实例 - Kivy: Invalid instance in App.root Kivy App Error-App.root中的实例无效 - Kivy App Error- Invalid instance in App.root 例外:当返回 2 个值时,kivy 中的 App.root 中的实例无效 - Exception: Invalid instance in App.root in kivy when returning 2 values 使用app.root引用.kv文件中的属性blabla - Referencing property in .kv file using app.root blabla 为什么 Kivy App.root 实例在 Pyinstaller 构建后无法识别 root 成员方法? - Why Kivy App.root instance does not recognize root member method after built by Pyinstaller? Python Kivy 接收 [严重] App.root 必须是 Widget 的_实例_ - Python Kivy Receiving [CRITICAL] App.root must be an _instance_ of Widget 为什么在 kivy python 中出现错误“App.root 中的实例无效”? - why do I get the error 'Invalid instance in App.root' in kivy python? 建立 Root Widget/以 app.root 身份访问它? - Establishing Root Widget / accessing it as app.root? 卸载和安装 python 和 pycharm 后运行简单 kivy 程序时出错:引发异常(“App.root 中的实例无效”) - error in running a simple kivy program after uninstalling and installing python and pycharm error:raise Exception('Invalid instance in App.root')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM