问题描述
我在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_easing()完成,disable()中的t.isAlive()也为True.问题出在哪里?
but this dose not work, t.isAlive() in disable() even when Time_consuming() finishes, is True. where is the problem ?
question2:另一个问题是Clock.schedule_interval将永远运行.函数完成后如何停止?
question2: another problem is, Clock.schedule_interval will continue to run for ever. how can stop it when function finished?
推荐答案
我发现:
question1:传递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按钮,直到功能完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!