我正在编写一个wxPython应用程序,它将进行大量的数据分析和显示。到目前为止,当两个线程试图同时更改GUI中的某些内容时,我的书写方式导致了问题。我想要做的是设置在主线程上运行的自己的简单队列,以便确保一次进行一次UI更新。
不过,我很难理解如何设置事件循环。一般来说,您会做类似的事情
while True:
try:
callback = queue.get(False)
except Queue.Empty:
break
callback()
我假设如果我按原样运行该代码,则WX将无法执行其操作,因为它将永远不会收到任何事件或任何东西,因为控制不会离开我的无限循环。如何使这种结构与WX事件循环共存?或更笼统地说,在WX应用程序中,如何确保某个任务仅在主线程上运行?
您可以使用wx.callafter,它接受一个可调用对象,该对象在当前事件和未决事件处理程序完成后在guis主循环中被调用。调用时,任何额外的位置或关键字args都会传递给可调用对象。
这是在运行单独的线程并更新主线程中的GUI时利用wx.CallAfter的gui代码示例。
该代码是由Andrea Gavana编写的,可在wxpython Phoenix文档中找到
#!/usr/bin/env python
# This sample shows how to take advantage of wx.CallAfter when running a
# separate thread and updating the GUI in the main thread
import wx
import threading
import time
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, title='CallAfter example')
panel = wx.Panel(self)
self.label = wx.StaticText(panel, label="Ready")
self.btn = wx.Button(panel, label="Start")
self.gauge = wx.Gauge(panel)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.label, proportion=1, flag=wx.EXPAND)
sizer.Add(self.btn, proportion=0, flag=wx.EXPAND)
sizer.Add(self.gauge, proportion=0, flag=wx.EXPAND)
panel.SetSizerAndFit(sizer)
self.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self, event):
""" This event handler starts the separate thread. """
self.btn.Enable(False)
self.gauge.SetValue(0)
self.label.SetLabel("Running")
thread = threading.Thread(target=self.LongRunning)
thread.start()
def OnLongRunDone(self):
self.gauge.SetValue(100)
self.label.SetLabel("Done")
self.btn.Enable(True)
def LongRunning(self):
"""This runs in a different thread. Sleep is used to
simulate a long running task."""
time.sleep(3)
wx.CallAfter(self.gauge.SetValue, 20)
time.sleep(5)
wx.CallAfter(self.gauge.SetValue, 70)
time.sleep(4)
wx.CallAfter(self.OnLongRunDone)
if __name__ == "__main__":
app = wx.App(0)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句