WXPYTHON棘手

WXPYTHON tricky

我有一个 textctrl 框 - 我每 1 秒连续读取一次数据。我有一个按钮,当值低于 50 时必须启用它。我有一段代码使 GUI 无响应。在我在这里展示的代码中,我一直等到值小于 50。然后启用开始按钮

    while self.pressure_text_control.GetValue()>50:
        self.start.Disable()
        time.sleep(1)
    self.start.Enable()

整个代码都在另一个按钮事件中。

   def OnDone(self, event):
        self.WriteToControllerButton([0x04])
        self.status_text.SetLabel('PRESSURE CALIBRATION DONE \n DUMP PRESSURE')
        self.led1.SetBackgroundColour('GREY')
        self.done.Disable()
        self.add_pressure.Disable()
        while self.pressure_text_control.GetValue()>50:
            self.start.Disable()
            time.sleep(1)
        self.start.Enable()

pressure_text_control 中的值每 1 秒更新一次。

设置一个 wx.timer 来为您完成工作,这将释放 GUI 主循环,即使其响应。
wx.Timer class 允许您以指定的时间间隔执行代码。 https://wxpython.org/Phoenix/docs/html/wx.Timer.html

    self.timer = wx.Timer(self)
    self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
    self.timer.Start(1000)

def OnTimer(self, event):
    "Check the value of self.pressure_text_control here and do whatever"

关闭程序前别忘了self.timer.Stop()