您可以使用 wx.callafter,它需要一个可调用对象,该对象在当前和挂起的事件处理程序完成后在 guis 主循环中调用。任何额外的位置或关键字参数都会在调用时传递给可调用对象。
下面是一个 GUI 代码的示例,它在运行单独的线程并在主线程中更新 GUI 时利用了 wx.CallAfter。
该代码由 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()