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


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01