Skip to main content
 首页 » 编程设计

Python之命令执行的 wxPython 序列

2025年04月02日8fff_TT

我有一个正常工作的代码,我知道我没有粘贴足够的代码 - 但我会用适当的注释解释每个命令。我的问题是为什么代码的行为而不是我期望的行为。

我的代码:

  def OnReset(self, event): # A function event which works after a button is pressed 
 
        self.reset_pump.Disable() # Disables the button so it is clicked 
        self.WriteToController([0x30],'GuiMsgIn') # Sends the reset command 
        self.flag_read.set() # Set Event of the thread 
        time.sleep(0.25) 
        self.tr.join() # Joining a Thread 
 
        self.MessageBox('Pump RESET going on Click OK \n') 
        # Having the above step is useful 
        # The question I have is based on the commands from here:     
        self.offset_text_control.Clear() 
        self.gain_text_control.Clear() 
        self.firmware_version_text_control.Clear() 
        self.pump_rpm_text_control.Clear() 
        self.pressure_text_control.Clear() 
        self.last_error_text_control.Clear() 
        self.error_count_text_control.Clear() 
        self.pump_model_text_control.Clear() 
        self.pump_serial_number_text_control.Clear() 
        self.on_time_text_control.Clear() 
        self.job_on_time_text_control.Clear() 
        # The above commands clear various widgets on my GUI.  
 
        self.ser.close() # Closes my serial connection to MCU 
        time.sleep(5) 
 
        self.OnCorrectComPort(event) # An external function which gets executed. This function has a Message BOX - which says - PORT IS OPENED. 
 
        return 

我预计,一旦线程加入 - 我的命令将清除 GUI。然后使用 (ser.close()) 关闭串行连接。然后 self.OnCorrectComPort(event) 被执行。

这就是我所看到的:线程与 tr.join() 连接,然后 self.OnCorrecComPort(event) 被执行,因为我可以看到带有“PORT OPENED”的消息框出现,我单击“确定”,然后我的 GUI 被清除.据我了解,这是错误的,请任何人纠正我。

请您参考如下方法:

问题是您在回调中调用 time.sleep(5) 和 self.OnCorrectComPort(),在返回之前到处理事件的主循环。

在您退出回调进入 wx 主循环之前,小部件不会反射(reflect)您的 Clear 调用的效果。

发生的事情是,您调用的例程被执行(由于 time.sleep 调用需要几秒钟,然后 wx 开始处理图形命令,并且小部件此时被清除(为时已晚,GUI 似乎停留在之前的状态)

如果您想要相反的方式,您可以使用 wx.CallAfter() 让 wx 有机会在调用例程之前处理它的事件。

在您的情况下,由于您想等待 5 秒,风险是再次卡住您的界面。在这种情况下,延迟 5 秒调用 wx.CallLater() 会更好,让 wx 有时间刷新所有小部件。

修改后的代码:

  def OnReset(self, event): # A function event which works after a button is pressed 
 
        self.reset_pump.Disable() # Disables the button so it is clicked 
        self.WriteToController([0x30],'GuiMsgIn') # Sends the reset command 
        self.flag_read.set() # Set Event of the thread 
        time.sleep(0.25) 
        self.tr.join() # Joining a Thread 
 
        self.MessageBox('Pump RESET going on Click OK \n') 
        # Having the above step is useful 
        # The question I have is based on the commands from here:     
        self.offset_text_control.Clear() 
        self.gain_text_control.Clear() 
        self.firmware_version_text_control.Clear() 
        self.pump_rpm_text_control.Clear() 
        self.pressure_text_control.Clear() 
        self.last_error_text_control.Clear() 
        self.error_count_text_control.Clear() 
        self.pump_model_text_control.Clear() 
        self.pump_serial_number_text_control.Clear() 
        self.on_time_text_control.Clear() 
        self.job_on_time_text_control.Clear() 
        # The above commands clear various widgets on my GUI.  
        self.ser.close() # Closes my serial connection to MCU 
        # will call calledAfter after 5 seconds 
        wx.CallLater(5000,self.calledAfter,[ser,event]) 
 
  def calledAfter(self,ser,event): 
        self.OnCorrectComPort(event) # An external function which gets executed. This function has a Message BOX - which says - PORT IS OPENED.