Python kivy,无法访问app.root属性
我在main.py和expense.kv中有以下代码,如下所示 main.pyPython kivy,无法访问app.root属性,python,kivy,Python,Kivy,我在main.py和expense.kv中有以下代码,如下所示 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.screen
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()
千伏
<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'
我知道这是因为我在我的spend.kv中引用了app.root.currency
文件:
:
文本:app.root.currency+“0.0”
那么,有没有一种方法可以在不收到此错误的情况下正确引用此引用?如果使用以下代码:
<Amount@Label>:
text: str(root) #
在此层次结构中,root
对象被定义为First
类的对象,它实际上是Screen
小部件的一个实例,因此您必须使用manager
属性才能访问manager
类:
<First>:
GridLayout:
cols: 1
Label:
text: root.manager.currency + '0.0'
:
网格布局:
科尔斯:1
标签:
文本:root.manager.currency+'0.0'
如果您使用以下代码:
<Amount@Label>:
text: str(root) #
在此层次结构中,root
对象被定义为First
类的对象,它实际上是Screen
小部件的一个实例,因此您必须使用manager
属性才能访问manager
类:
<First>:
GridLayout:
cols: 1
Label:
text: root.manager.currency + '0.0'
:
网格布局:
科尔斯:1
标签:
文本:root.manager.currency+'0.0'
您可能希望将货币
设置为字符串属性
,以便在货币
更改时自动更新标签文本。您可能希望将货币
设置为字符串属性
,以便在货币
更改时自动更新标签文本。
<First>:
GridLayout:
cols: 1
Label:
text: root.manager.currency + '0.0'