【发布时间】:2015-01-28 13:58:07
【问题描述】:
我想在运行某些东西时更新 kivy 小部件的属性...
例子:
class app(App):
def build(self):
self.layout = Layout()
self.name = Label(text = "john")
self.layout.add_widget(self.name)
return self.layout
def update(self):
for i in range(50): #keep showing the update
self.name.text = str(i)
#maybe some sleep here
obj = app()
obj.run()
obj.update()
这只会显示循环的最终结果。我想在循环进行时继续更新 label.text。
我寻找了类似 bind()、setter() 和 ask_update() 函数,但如果是这些函数,我不知道如何使用它们。
------------------ 编辑 -----------------------
尝试适应inclement 答案(使用时钟在其他线程中运行更新功能),我得到下面的代码试图遵循我的问题的真实想法,但仍然无法正常工作:
class main():
def __init__(self, app):
self.app = app
... some code goes here ...
def func(self):
Clock.schedule_once(partial(self.app.update, self.arg_1, self.arg_2), 0)
class app(App):
def build(self):
self.main = main(self)
self.layout = Layout()
self.name = Label(text = "john")
self.layout.add_widget(self.name)
return self.layout
... some code goes here ...
def update(self, dt, arg_1, arg_2):
self.name = arg_1
sleep(5)
self.name = arg_2
obj = app()
obj.run()
我需要调用func函数,并在我命令update函数中的文本更改时准确地更新标签文本。
【问题讨论】: