必需:按下guizero应用程序上的按钮应该首先设置一个文本值,然后执行一个函数(这里是一个简单的时间。睡眠();原来是子流程);执行该函数后,应显示结果文本
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from guizero import App, PushButton, Text
import subprocess
import time
# Action you would like to perform
def counter0():
global s0
text0.value = int(text0.value) + 1 # works
text1.value = s0 # works
def counter1():
global s0
text1.value = s0 # display a status value - but does not (!)
time.sleep(1) # originally a subprocess is called here; works
text1.value = "ready" # only diplays this after a delay
s0="start something after pressing butt1"
app = App("Hello world", layout="grid")
text0 = Text(app, text="1", align="left", grid=[0,1])
text1 = Text(app, text=s0, align="left",grid=[0,8] )
butt1 = PushButton(app, text="Button", command=counter1, align="left", grid=[0,2])
text0.repeat(10000, counter0) # Schedule call to counter() every 1000ms
app.display()
很可能我不理解guizero背后的想法。你知道如何管理这些需求吗?你知道吗
如果我正确理解你的问题,你的具体问题是:
这不起作用,因为
guizero
框架正在同步执行函数:在函数返回之前,没有其他代码执行,包括更新显示的代码。你知道吗如果您想:
subprocess
运行命令之前显示消息然后你需要重写你的逻辑,这样你的应用程序就不会等待“counter1”完成了。异步运行代码的一个选项是在单独的线程中运行它。例如:
运行上述代码将产生以下行为:
相关问题 更多 >
编程相关推荐