kivy python 时钟时间表更新
kivy python clock schedule update
'main_loop' 函数 while 循环不会每 5 秒递增 self.i 值,'run' 函数不会每 1 秒递减 self.mycounter 值。
为什么?
我做错了什么?
我不想使用 time.sleep。
class MyThread(BoxLayout):
stop = []
timer = []
times = []
i = NumericProperty(0)
mycounter = NumericProperty(0)
def incrementi(self, *args):
self.i += 1
def decrementcounter(self,*args):
self.mycounter -= 1
def run(self):
self.mycounter = 30
while not self.stop:
self.timer.append(self.mycounter)
Clock.schedule_interval(self.decrementcounter, 1.0)
self.times.append(self.mycounter)
return self.mycounter
def main_loop(self):
self.i = 0
while True:
Clock.schedule_interval(self.incrementi, 5.0)
if self.i == 2:
self.mycounter = 30
threading.Thread(target = self.run).start()
if self.i == 5:
self.stop.append('dummystring')
if self.i == 6:
self.stop.pop(0)
self.timer = []
self.ids.lbl.text = "{}".format(self.i)
if self.i == 7:
self.i = 0
def read_it(self):
threading.Thread(target = self.main_loop).start()
class MyApp(App):
def build(self):
self.load_kv('thread.kv')
return MyThread()
if __name__ == "__main__":
app = MyApp()
app.run()
kivy.pv
<MyThread>:
Button:
text: "start program"
on_release: root.read_it()
Label:
id: lbl
text: "current step"
font_size: 50
如果没有必要,我建议您放弃整个 while
样式 - 尤其是 main_loop
中的那个样式,它只是检查值。如果你真的不需要那个功能,它在 kivy 中不好,例如:
while <some list>:
value = <some list>.pop()
# do something
甚至这里还有一些不同的方法。正如 inclement 所建议的那样,使用 Clock
是正确的方法。在那个特定循环中还有一点 错误 细节是这样的:
while True:
Clock.schedule_interval(self.incrementi, 5.0)
这将无限次安排间隔,这基本上会在您 运行 此函数时冻结您的应用程序,或者(如果 运行 在 Thread 中)导致无休止的 loop/freeze + 创建一个大内存吞噬者 = 它会禁止你 Thread.set()
即停止线程并正确退出应用程序,最终会崩溃。
我希望这是错字,因为你只需要将它放在 while
循环之上即可。另一件事:decrementcounter
... 它处于 while
循环中,它会再次创建多个时钟并引起麻烦。同样的事情 - 再一次,将它放在循环之上以使其工作。
基本上 while
加上 time.sleep(t)
就是 Clock.schedule_interval(some_function, t)
。很高兴您想检查一些东西,但是每个 Clock.schedule_*
您都创建了另一个时钟,我没有看到任何阻止它的东西。所以基本上你会创建无限多的时钟。
更类似于 while
only(without sleep) 的是 Clock.schedule_interval(some_function)
即你省略了时间参数,它会在每一帧调用函数,如果 60fps
,那么每秒 60 次。对于简单的值检查来说非常精确。
'main_loop' 函数 while 循环不会每 5 秒递增 self.i 值,'run' 函数不会每 1 秒递减 self.mycounter 值。
为什么?
我做错了什么?
我不想使用 time.sleep。
class MyThread(BoxLayout):
stop = []
timer = []
times = []
i = NumericProperty(0)
mycounter = NumericProperty(0)
def incrementi(self, *args):
self.i += 1
def decrementcounter(self,*args):
self.mycounter -= 1
def run(self):
self.mycounter = 30
while not self.stop:
self.timer.append(self.mycounter)
Clock.schedule_interval(self.decrementcounter, 1.0)
self.times.append(self.mycounter)
return self.mycounter
def main_loop(self):
self.i = 0
while True:
Clock.schedule_interval(self.incrementi, 5.0)
if self.i == 2:
self.mycounter = 30
threading.Thread(target = self.run).start()
if self.i == 5:
self.stop.append('dummystring')
if self.i == 6:
self.stop.pop(0)
self.timer = []
self.ids.lbl.text = "{}".format(self.i)
if self.i == 7:
self.i = 0
def read_it(self):
threading.Thread(target = self.main_loop).start()
class MyApp(App):
def build(self):
self.load_kv('thread.kv')
return MyThread()
if __name__ == "__main__":
app = MyApp()
app.run()
kivy.pv
<MyThread>:
Button:
text: "start program"
on_release: root.read_it()
Label:
id: lbl
text: "current step"
font_size: 50
如果没有必要,我建议您放弃整个 while
样式 - 尤其是 main_loop
中的那个样式,它只是检查值。如果你真的不需要那个功能,它在 kivy 中不好,例如:
while <some list>:
value = <some list>.pop()
# do something
甚至这里还有一些不同的方法。正如 inclement 所建议的那样,使用 Clock
是正确的方法。在那个特定循环中还有一点 错误 细节是这样的:
while True:
Clock.schedule_interval(self.incrementi, 5.0)
这将无限次安排间隔,这基本上会在您 运行 此函数时冻结您的应用程序,或者(如果 运行 在 Thread 中)导致无休止的 loop/freeze + 创建一个大内存吞噬者 = 它会禁止你 Thread.set()
即停止线程并正确退出应用程序,最终会崩溃。
我希望这是错字,因为你只需要将它放在 while
循环之上即可。另一件事:decrementcounter
... 它处于 while
循环中,它会再次创建多个时钟并引起麻烦。同样的事情 - 再一次,将它放在循环之上以使其工作。
基本上 while
加上 time.sleep(t)
就是 Clock.schedule_interval(some_function, t)
。很高兴您想检查一些东西,但是每个 Clock.schedule_*
您都创建了另一个时钟,我没有看到任何阻止它的东西。所以基本上你会创建无限多的时钟。
更类似于 while
only(without sleep) 的是 Clock.schedule_interval(some_function)
即你省略了时间参数,它会在每一帧调用函数,如果 60fps
,那么每秒 60 次。对于简单的值检查来说非常精确。