wxCallAfter for C++

Robin Dunn robin at alldunn.com
Wed Jun 6 08:31:48 PDT 2007


Robert Roebling wrote:
> 
> What does it do?
> 

It takes a reference to a callable object, a sequence and/or dictionary 
of parameters to pass to that callable, puts them all in a event object, 
and posts the event to the app object.  When the event arrives the 
callable is called passing it the specified args.  It's *very* flexible 
because of Python's abilities, and is *very* useful.  Probably about a 
quarter of the wxPython questions I answer contain the phrase "Use 
wx.CallAfter to..."  There is also wx.CallLater which uses a timer to do 
basically the same thing.



def CallAfter(callable, *args, **kw):
     """
     Call the specified function after the current and pending event
     handlers have been completed.  This is also good for making GUI
     method calls from non-GUI threads.  Any extra positional or
     keyword args are passed on to the callable when it is called.

     :see: `wx.CallLater`
     """
     app = wx.GetApp()
     assert app is not None, 'No wx.App created yet'

     if not hasattr(app, "_CallAfterId"):
         app._CallAfterId = wx.NewEventType()
         app.Connect(-1, -1, app._CallAfterId,
                     lambda event: event.callable(*event.args, **event.kw) )
     evt = wx.PyEvent()
     evt.SetEventType(app._CallAfterId)
     evt.callable = callable
     evt.args = args
     evt.kw = kw
     wx.PostEvent(app, evt)


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wx-users mailing list