1

我正在使用 guizero(在 Raspberry Pi 上运行)编写 Python 程序,用户可以选择饮料并使用 RFID 芯片进行识别。然后将事务存储在远程设备上的 mariadb 中。

这个想法是,用户选择一种饮料,然后屏幕会改变 10 秒以提示用户使用他的 RFID 芯片进行身份验证。如果他没有,软件应该返回到主屏幕。

到目前为止,一切正常,但我在使用 GUI 时遇到了问题。由于 10 Second-Scan-Period 是一个 while-Loop,它会冻结整个 gui 并且不显示提示,并且让用户不知道他必须做什么。

我试过的:

  • 我使用 Thread-Object 来调用扫描函数,但这导致提示消失得非常快。
  • 我尝试了回调,但这意味着无论如何都要冻结我的 gui
  • 我尝试了 App-Object 的重复方法并删除了 while 循环,但这意味着系统正在不停地扫描,这不是故意的。

这是一个小示例代码,它以非常简单的方式描述了我的程序:

from guizero import App, Text, PushButton
explanation = "To scan your chip, please click on the button."

def scan():
    text.value = "Scanning...."
    #set the endtime to ten seconds from now
    endtime = time.time() + 10
    
    #repeat for ten seconds
    while time.time() < endtime:
        print("Scanning for RFID")
    print("End of scan")
    text.value = explanation
        
        
app = App("Funny Title Here")

#initial welcome text
text = Text(app,text=explanation)

#if button is clicked, change the text and scan for ten seconds
button = PushButton(app, command=scan, text="Scan")

app.display()

我知道我尝试过的一种方法是正确的,但我似乎缺乏所需的逻辑。所以我的问题是:我怎样才能实现,用户按下按钮后 gui 更新,开始扫描 RFID 卡,10 秒后停止并返回原始视图?

谢谢

4

1 回答 1

0

我找到了解决办法!这是我解决它的方法:想法是每 100 毫秒调用一次扫描函数。当用户单击按钮时,布尔值设置为 true,并且扫描功能会执行某些操作。如果布尔值是假的,那么什么也不会发生。

import time
from guizero import App, Text, PushButton
explanation = "To scan your chip, please click on the button."
isScan=False
counter = 0
endtime = 0

def scan():
    global isScan
    if isScan:
        global endtime
        global counter

        #repeat for ten seconds
        if time.time()<endtime:
            print(f"{counter}")
            counter += 1
        else:
            isScan=False
            print("End of scan")
            counter = 0 
            text.value = explanation
        
            
def scanScreen():
    global isScan
    global endtime
    text.value = "Scanning...."
    endtime = time.time()+10
    isScan = True
    
    
app = App("Funny Title Here")

#initial welcome text
text = Text(app,text=explanation)

#if button is clicked, change the text and scan for ten seconds
button = PushButton(app, command=scanScreen, text="Scan")
app.repeat(100,scan)
app.display()
于 2021-05-04T09:34:07.523 回答