wx.CallAfter takes a function and its arguments, and calls the function when the current event handler has exited. It is a safe way of requesting action of a Window from another thread. The code below has a couple of examples.
1 import threading,wx
2
3 ID_RUN=101
4 ID_RUN2=102
5
6 class MyFrame(wx.Frame):
7 def __init__(self, parent, ID, title):
8 wx.Frame.__init__(self, parent, ID, title)
9 panel = wx.Panel(self, -1)
10 mainSizer=wx.BoxSizer(wx.HORIZONTAL)
11 mainSizer.Add(wx.Button(panel, ID_RUN, "Click me"))
12 mainSizer.Add(wx.Button(panel, ID_RUN2, "Click me too"))
13 panel.SetSizer(mainSizer)
14 mainSizer.Fit(self)
15 wx.EVT_BUTTON(self, ID_RUN, self.onRun)
16 wx.EVT_BUTTON(self, ID_RUN2, self.onRun2)
17
18 def onRun(self,event):
19 print "Clicky!"
20 wx.CallAfter(self.AfterRun, "I don't appear until after OnRun exits")
21 s=raw_input("Enter something:")
22 print s
23
24 def onRun2(self,event):
25 t=threading.Thread(target=self.__run)
26 t.start()
27
28 def __run(self):
29 wx.CallAfter(self.AfterRun, "I appear immediately (event handler\n"+ \
30 "exited when OnRun2 finished)")
31 s=raw_input("Enter something in this thread:")
32 print s
33
34 def AfterRun(self,msg):
35 dlg=wx.MessageDialog(self, msg, "Called after", wx.OK|wx.ICON_INFORMATION)
36 dlg.ShowModal()
37 dlg.Destroy()
38
39
40 class MyApp(wx.App):
41 def OnInit(self):
42 frame = MyFrame(None, -1, "CallAfter demo")
43 frame.Show(True)
44 frame.Centre()
45 return True
46
47 app = MyApp(0)
48 app.MainLoop()
When "Click me" is pressed, onRun is called. It immediately prints "Clicky!" and issues three calls to wx.CallAfter, then waits for you to enter some text via stdin, prints the text, and returns. At this point, all pending events have been dealt with, and the commands given to CallAfter are executed. Note that the dialogs appear in reverse order.
When "Click me too" is pressed, onRun2 is called. It spawns a thread and returns. The thread executes __run, which calls CallAfter, waits for you to enter some text, prints it, and returns. In this case, however, the dialog now appears before you enter the text. This is because the event handler has already exited; the dialog was created from a thread. You can type at the prompt or close the dialog, whichever you prefer. You can even close the dialog and click on a button again before typing.
If CallAfter is not used in this second circumstance, crashes occur. Change
wx.CallAfter(self.AfterRun, "I appear immediately (event handler\n"+ \ "exited when OnRun2 finished)")
to
self.AfterRun("I appear immediately (event handler\n"+ \ "exited when OnRun2 finished)")
Run the program and click on "Click me too". Dismiss the dialog box and click either button again (before typing anything) to witness the event dispatch thread becoming very confused.