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 中完成


基础教程推荐
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01