我正在使用 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 秒后停止并返回原始视图?
谢谢