un_active button until function finished in kivy(un_active 按钮,直到函数在 kivy 中完成)
问题描述
我正在为 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 中完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:un_active 按钮,直到函数在 kivy 中完成


- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01