这是我最终使用的代码示例。这是 Mike Driscoll 和 Yoriz 的答案的组合。他们的回答对于所提出的问题都很好且有效。但是,只要旋转字符需要指示正在进行的计算,它们就会停止工作。为了防止这种情况,我基本上启动了两个线程,一个处理实际计算,另一个处理旋转字符。当计算完成时,它的线程向主框架发送一个事件。后者可以使用此事件来报告计算结果并中止进度线程。进度线程反过来定期发布事件(由 wx.Timer 调解)以更新 TextCtrl。
代码如下:
import wx
from threading import Thread
from itertools import cycle
# Define notification event for thread completion
EVT_RESULT_ID = wx.NewId()
EVT_PROGRESSUPDATE_ID = wx.NewId()
def EVT_RESULT(win, func):
"""Define Result Event."""
win.Connect(-1, -1, EVT_RESULT_ID, func)
class ResultEvent(wx.PyEvent):
"""Simple event to carry arbitrary result data."""
def __init__(self, data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_RESULT_ID)
self.data = data
def EVT_PROGRESSUPDATE(win, func):
win.Connect(-1,-1,EVT_PROGRESSUPDATE_ID,func)
class ProgressUpdateEvent(wx.PyEvent):
def __init__(self,data):
wx.PyEvent.__init__(self)
self.SetEventType(EVT_PROGRESSUPDATE_ID)
self.data = data
# Thread class that shows progress
class ProgressThread(Thread):
def __init__(self,notify_window):
Thread.__init__(self)
self._notify_window = notify_window
self.chars = cycle(('|','/','-','\\'))
self.tinyTimer = wx.Timer(notify_window)
notify_window.Bind(wx.EVT_TIMER, self.updateTextCtrl, self.tinyTimer)
self.tinyTimer.Start(100)
def updateTextCtrl(self,event):
wx.PostEvent(self._notify_window, ProgressUpdateEvent(next(self.chars)))
def abort(self):
self.tinyTimer.Stop()
return
# Thread class that executes processing
class WorkerThread(Thread):
"""Worker Thread Class."""
def __init__(self, notify_window):
"""Init Worker Thread Class."""
Thread.__init__(self)
self._notify_window = notify_window
self.start()
def run(self):
"""Run Worker Thread."""
x = 0
for i in range(100000000):
x += i
wx.PostEvent(self._notify_window, ResultEvent(x))
class MainFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Thread Test')
self.start_button = wx.Button(self, -1, 'Start', pos=(0,0))
self.progress = wx.TextCtrl(self,-1,'',pos=(0,50))
self.status = wx.StaticText(self, -1, '', pos=(0,100))
self.Bind(wx.EVT_BUTTON, self.OnStart, self.start_button)
# Set up event handlers
EVT_RESULT(self,self.OnResult)
EVT_PROGRESSUPDATE(self,self.OnProgressUpdate)
# And indicate we don't have a worker thread yet
self.worker = None
def OnStart(self, event):
"""Start Computation."""
# Trigger the worker thread unless it's already busy
if not self.worker:
self.status.SetLabel('Starting computation')
self.worker = WorkerThread(self)
self.p = ProgressThread(self)
def OnResult(self, event):
"""Show Result status."""
self.p.abort()
self.status.SetLabel('Computation Result: %s' % event.data)
self.worker = None
def OnProgressUpdate(self,event):
self.progress.ChangeValue(event.data)
class MainApp(wx.App):
def OnInit(self):
self.frame = MainFrame(None, -1)
self.frame.Show(True)
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = MainApp(0)
app.MainLoop()