[wxPython-users] using lambda to pass args to event function
Christopher Barker
Chris.Barker at noaa.gov
Mon Nov 20 17:08:52 PST 2006
TiNo wrote:
>> def makeArgsHandler(handler, arg):
>> return lambda event: handler(event, arg)
> 2006/11/20, Christopher Barker <Chris.Barker at noaa.gov>:
>> 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.
>
> I use it this way :D :
> --------------------------------------
> #Set button events
> self.Bind(wx.EVT_BUTTON,
> makeArgsHandler(controller.PopulateMainList,'pkl'),
> id=ID_PKL_BUTTON)
which could be:
self.Bind(wx.EVT_BUTTON,
lambda event: controller.PopulateMainList(event, arg='pkl'),
id=ID_PKL_BUTTON)
Thus relieving you of the need to have the makeArgsHandler() function at
all.
> referencing to the button itself is not that easy. Bind is called in
> main class. Button is located in gui class, in a subclass, in a panel,
> in a sizer, etc. I find this easier. If I decide to put the button in
> another panel I don't have to change these binds.
That does sound like a flexible structure, but:
I see a lot of explicit naming: ID_PKL_BUTTON, 'pkl', etc. It looks like
there could be some cleaning up there. Maybe all these buttons should go
in a list or dict, rather than keeping them all around my name.
Also, for me, Event binding is very much a GUI function -- why not have
it with the GUI code?
Who knows, I'd have to see the whole app structure to be sure.
-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