Python - wxPython 命令执行顺序

Python - wxPython Sequence of Command Execution

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

我的代码:

  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 主循环之前,小部件不会反映您的 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.