0
我在main.py和spend.kv以下代碼如下kivy,斜面訪問app.root財產
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
<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
<[email protected]>:
text: app.root.currency + '0.0'
當我運行此程序崩潰與以下錯誤:
AttributeError: 'NoneType' object has no attribute 'currency'
我知道這是因爲我的全球化志願服務青年的我spend.kv 文件app.root.currency:
<[email protected]>:
text: app.root.currency + '0.0'
那麼有沒有辦法正確地作出此引用,而不會收到此錯誤?
您可能希望'currency'一個'StringProperty',使標籤文本得到更新時自動的'currency'改變。 – zeeMonkeez