[英]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.