我正在尝试构建一个非常简单的 wxPython GUI 来监视和显示外部数据。有一个按钮可以打开/关闭监控。打开监控后,GUI 会使用实时数据更新几个 wx StaticLabels。关闭监视时,GUI 空闲。
我尝试构建它的方式是使用相当简单的 Python 线程布局。单击“开始监控”按钮时,程序会生成一个线程,用实时信息更新标签。单击“停止监控”按钮时,将调用 thread.join(),它应该停止。
启动功能有效,实时数据更新效果很好,但是当我单击“停止”时,整个程序冻结。我在 Windows 7 64 位上运行它,所以我得到了通常的“此程序已停止响应”Windows 对话框。
以下是相关代码:
class MonGUI(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
...
... other code for the GUI here ...
...
# Create the thread that will update the VFO information
self.monThread = Thread(None, target=self.monThreadWork)
self.monThread.daemon = True
self.runThread = False
def monThreadWork(self):
while self.runThread:
...
... Update the StaticLabels with info
... (This part working)
...
# Turn monitoring on/off when the button is pressed.
def OnClick(self, event):
if self.isMonitoring:
self.button.SetLabel("Start Monitoring")
self.isMonitoring = False
self.runThread = False
self.monThread.join()
else:
self.button.SetLabel("Stop Monitoring")
self.isMonitoring = True
# Start the monitor thread!
self.runThread = True
self.monThread.start()
我确信有更好的方法可以做到这一点,但我对 GUI 编程和 Python 线程还很陌生,这是我想出的第一件事。
那么,为什么单击按钮停止线程会使整个事情冻结?