Python - wxPython命令执行序列

时间:2016-10-07 19:32:26

标签: python multithreading wxpython

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

我的代码:

  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(事件)。

这就是我所看到的:线程与tr.join()连接然后self.OnCorrecComPort(事件)被执行,因为我可以看到带有" PORT OPENED"的消息框。出现,我点击确定,然后我的GUI被清除。据我所知,这是错误的,任何人都请指正。

1 个答案:

答案 0 :(得分:1)

问题是您在回调中调用了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.