我正在为 UI 使用 kivy.有一个耗时的函数,当它运行时,kivy ui 会变黑,所以我使用了线程.我想禁用按钮,直到耗时功能完成,然后再次启用按钮.我一直在使用类似下面的东西:
I am using kivy for UI. there is a Time_consuming function and when it runs, kivy ui will goes in black, so I used Threading. I want to disable buttons until Time_consuming function finishes, and then enable button again. I have been used something like below:
from threading import Thread
from kivy.clock import Clock
from functools import partial
def run():
self.run_button.disabled=True
self.back_button.disabled=True
t=Thread(target=Time_consuming(), args=())
t.start()
Clock.schedule_interval(partial(disable, t.isAlive()), 8)
def disable(t, what):
print(t)
if not t:
self.run_button.disabled=False
self.back_button.disabled=False
但这不起作用,即使 Time_sumption() 完成,disable() 中的 t.isAlive() 也是 True.问题出在哪里?
but this dose not work, t.isAlive() in disable() even when Time_consuming() finishes, is True. where is the problem ?
问题2:另一个问题是,Clock.schedule_interval 会一直运行下去.功能完成后如何停止?
question2: another problem is, Clock.schedule_interval will continue to run for ever. how can stop it when function finished?
我发现:
问题1:传递 t 而不是 t.isAlive().this :
question1: pass t instead of t.isAlive().this :
Clock.schedule_interval(partial(disable, t.isAlive()), 8)
改为:
Clock.schedule_interval(partial(disable, t), 8)
问题2:如果disable()返回False,调度将被取消,不再重复.
question2: If the disable() returns False, the schedule will be canceled and won’t repeat.
def run_model():
self.run_button.disabled=True
self.back_button.disabled=True
t=Thread(target=run, args=())
t.start()
Clock.schedule_interval(partial(disable, t), 8)
def disable(t, what):
if not t.isAlive():
self.run_button.disabled=False
self.back_button.disabled=False
return False
这篇关于un_active 按钮,直到函数在 kivy 中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!