你可以使用wx.callafter,它需要一個可調用的對象,在當前和未決事件處理程序完成後在guis主循環中調用。任何額外的位置或關鍵字參數在調用時會傳遞給可調用對象。
下面是一個GUI代碼的例子,它在運行一個單獨的線程並在主線程中更新GUI時利用了wx.CallAfter。
的代碼是由安德烈Gavana這是在wxpython Phoenix docs
#!/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()
使用callafter - http://wxpython.org/Phoenix/docs/html/functions.html?highlight=call%20after#CallAfter,這樣,您的通話將被添加到隊列mainloops。 – Yoriz
CallAfter和CallLater在這裏是你的朋友。此外,讓這個線程「睡眠」也會給wx一個處理事情的機會。 –
@Yoriz你可以把它變成答案嗎? – bdesham