0

我是 python 和一般编程的新手,我正在尝试使用 Pytransitions 的状态机实现来构建算法。

我试图找到一种方法在连接到 RPi 4 Model B 的小型外部 LCD 屏幕上显示文本,同时让程序运行 3 个状态,每个状态显示不同的消息。

这是我尝试过的一个例子,我很确定给我带来麻烦的是 win.mainloop(),但我不知道如何解决它。根据我在阅读文档后的理解,我必须将它包含在代码的末尾以生成窗口本身,但是从状态到状态的转换时的文本更新仍然无法按我的预期工作 - 它们根本不显示完全没有。

这是简化的 3 状态系统的代码:

from transitions import Machine
from tkinter import *
import time

win = Tk()
intvar = StringVar() #A tkinter string variable for labels
cntdwn = 10
intvar.set = 10

class Identification(object):
    def on_enter_A(self):
        print("We've just entered State A")
        update_window('System Started')
    def on_enter_B(self):
        print("We've just entered State B")
        update_window('Please look into the camera')
    def on_enter_C(self):
        print("We've just entered State C")
        update_window('Face and readings authorized')

def update_window(msg):
    label.configure(text=msg)

def rdy():
    #label= Label(win, text= "Sensors fully calibrated, please blow on the sensor", font=('Times New Roman bold',50))
    label.pack(padx=10, pady=10)
    label.configure(text="Sensors fully calibrated, please blow on the sensor")

def cntdwn_update(secs):
    if (secs):
        label.configure(text="Greetings,booting system & calibrating sensors " +str(secs))
        win.after(1000, cntdwn_update, secs-1)

SBS = Identification()
states = ['A', 'B', 'C'] #Set the states in which the machine operates
transitions = [
    { 'trigger': 'System_Ready', 'source': 'A', 'dest': 'B' },
    { 'trigger': 'Picture_Taken', 'source': 'B', 'dest': 'C' },
    { 'trigger': 'User_Verified', 'source': 'C', 'dest': 'A' }
    ] #Sets the transition parameter string for each state
machine = Machine(SBS, states=states, transitions = transitions, initial='A')

win.geometry("650x250") #Set the geometry
win.attributes('-fullscreen', True) #Create a fullscreen window
intvar.set = "Greetings, calibrating sensor (10s)"
label= Label(win, text= "Greetings, calibrating sensors " + str(cntdwn), font=('Times New Roman bold',50))
label.pack(padx=10, pady=10)
win.after(1000, cntdwn_update, cntdwn-1)
win.after(10000, rdy)
#win.after(12000, update_window, 'Please look into the camera')  #This works but I want this 
to activate when the state switches, not after an a set amount of time.

SBS.System_Ready()
SBS.Picture_Taken()
SBS.User_Verified()
win.mainloop()

发生的情况如下:

屏幕显示“System Started” 1 秒,然后显示“Sensors calibrating”消息,同时每秒自我更新,总共 10 秒,然后显示“Sensors 已完全校准,请对传感器吹气”。

它绝不会打印任何附加到状态更改事件的字符串,即“请查看相机”或“面部和读数已授权”。

我该如何解决这个问题?

任何帮助将非常感激!

4

0 回答 0