Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python guizero Raspberry Pi:按钮->命令序列问题_Python_Python 3.x_Tkinter_Raspberry Pi3_Guizero - Fatal编程技术网

Python guizero Raspberry Pi:按钮->命令序列问题

Python guizero Raspberry Pi:按钮->命令序列问题,python,python-3.x,tkinter,raspberry-pi3,guizero,Python,Python 3.x,Tkinter,Raspberry Pi3,Guizero,要求:按下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

要求:按下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背后的想法。有什么办法来管理这些需求吗?

如果我正确理解您的问题,您的具体问题如下:

    text1.value = s0       # display a status value - but does not (!)
这不起作用,因为guizero框架正在同步执行函数:在函数返回之前,不会执行其他代码(包括更新显示的代码)

如果您想:

在使用子流程运行命令之前显示消息 运行一些命令 在命令完成后显示消息 然后你们需要重写你们的逻辑,这样你们的应用程序就不会等待“计数器1”完成了。异步运行代码的一个选项是在单独的线程中运行代码。例如:

from guizero import App, PushButton, Text
import threading
import time


# Action you would like to perform
def counter0():
    global s0
    text0.value = int(text0.value) + 1  # works
    text1.value = s0                    # works


def run_command():
    time.sleep(1)          # originally a subprocess is called here; works
    text1.value = "ready"  # only diplays this after a delay


def counter1():
    global s0
    text1.value = "Running command..."
    threading.Thread(target=run_command).start()


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)

app.display()
运行上述代码将提供以下行为:


谢谢你,拉尔克斯,给了我一个快速而有用的回答。这正是我想要的。我想看看其他的方法,但只是为了满足我的好奇心。你建议的方法对我来说简单易懂,而且很短。顺便说一句:漂亮的动画gif;C