您缺少更新文本值的事件scoreLabel
你需要在你的MyClass
, 见下文:
class MyClass(EventDispatcher):
a = NumericProperty(0)
def on_a(self, instance, value):
app = App.get_running_app()
app.scoreLabel.text = str(value)
当财产a
更新,on_a
被触发并且then你可以用它来更新scoreLabel
值,否则它甚至没有连接。线路text = str(ins.a)
采取value来自ins.a
并使用它,即0
为你。
但是,您需要访问该scoreLabel
不知何故,这可能通过例如有用App.get_running_app() https://kivy.org/docs/api-kivy.app.html#kivy.app.App.get_running_app您可以在其中存储实例以供以后使用:
app = App.get_running_app()
app.scoreLabel = Label(text=str(ins.a), id='scorelabel', font_size=20)
content.add_widget(app.scoreLabel)
这样即使在on_a
event later。或者使用self
并访问Popup
直接与App.get_running_app().popup
然后是它的内容。
App.get_running_app() https://kivy.org/docs/api-kivy.app.html#kivy.app.App.get_running_app但是有时可能不是一个更好的选择,因此您甚至可以使用全局变量或其他某种方式来存储实例,例如在其他班级内。如果你有一个Popup
,该小部件将自身添加到根Window
例如,它存储在:
Window -> children -> Popup -> content -> layout -> scoreLabel
但要小心,因为直接弄乱 Window 可能会产生不幸的结果。