Hello
I've been writing some code to drive a GUI from a different thread. I found code using wx.CallAfter to be rather obscure, so I've written a decorator that does the work for you. I haven't seen this approach mentioned anywhere, so I thought I'd share it.
def safely(fun):
@functools.wraps(fun)
def wrapper(*args, **kwargs):
return wx.CallAfter(fun, *args, **kwargs)
return wrapper
Using this approach the example at http://wiki.wxpython.org/CallAfter becomes (with some brutal editing):
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
# snip
def onRun(self,event):
print "Clicky!"
self.AfterRun("I don't appear until after OnRun exits")
s=raw_input("Enter something:")
print s
# snip
def __run(self):
self.AfterRun("I appear immediately (event handler\n"+ \
"exited when OnRun2 finished)")
s=raw_input("Enter something in this thread:")
print s
@safely
def AfterRun(self,msg):
dlg=wx.MessageDialog(self, msg, "Called after", wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
Ben