[wxPython-users] using lambda to pass args to event function

Christopher Barker Chris.Barker at noaa.gov
Mon Nov 20 10:10:22 PST 2006


Ben North wrote:
>> def makeArgsHandler(handler, arg):
>>      return lambda event: handler(event, arg)

>    from functional import rcurry
>    self.Bind(wx.EVT_BUTTON,
>              rcurry(controller.PopulateMainList, 'pkl'),
>              id = ID_PKL_BUTTON)

pretty cool.

however, for an example this simple, you may be able to make use us 
default arguments:

     self.Bind(wx.EVT_BUTTON,
               lambda event: handler(event, arg=current_arg),
               id = ID_PKL_BUTTON)

now arg will get set to whatever current_arg is when that Bind call is 
executed -- i.e. in the Window __init__, usually.

By the way, I stay away from using explicit IDs. If you can store an ID, 
why not just a reference to the Button itself:

     self.Pkl_Button = wx.Button(......)
     selfPkl_Button.Bind(wx.EVT_BUTTON,
                         lambda event: handler(event, arg=arg))

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list