wxwindows event function arguments

Greg Krohn greg at invalid.invalid
Fri Apr 8 00:28:00 EDT 2005


lotmr wrote:
> I have a windows launch bar application that sits in the system tray.
> What I want to do is when i click on a button in the launch menu, it
> calls the event which then calls 'OnLaunch('path')' this does not seem
> possible. When I change 'OnLaunch(self, event)' to 'OnLaunch(self,
> event, path)' it says I dont have enough arguments. When I remove
> 'event', the OnLaunch runs as soon as the program loads. Using Google I
> can absolutly not find a way to pass arguments to a function called by
> a event. If indeed it is absolutly impossible, what could be the best
> alternate possibility?

One possible solution is to curry your event handler. This means that you
already supply the third argument (i.e. path) so that when wxWidgets
call the handler it only needs to supply the first two (i.e self and
event). If that's confusing see
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549 for more
info. So basically this is what you do. Put the following code before
your MainWindow class:

class curry:
     def __init__(self, fun, *args, **kwargs):
         self.fun = fun
         self.pending = args[:]
         self.kwargs = kwargs.copy()

     def __call__(self, *args, **kwargs):
         if kwargs and self.kwargs:
             kw = self.kwargs.copy()
             kw.update(kwargs)
         else:
             kw = kwargs or self.kwargs

         return self.fun(*(self.pending + args), **kw)

Then, when you bind your event, do it like this:

         EVT_MENU(self, wxID_FIREFOX, curry(self.OnLaunch, path='D:\Program Files\Mozilla Firefox\Firefox.exe'))

Then change your OnLaunch handler back to the way you originally wanted it:

     def OnLaunch (self, event, path):
         print event.GetString()
         os.startfile(path)


Hope this helps,
greg



More information about the Python-list mailing list