passing arguments to events

Dom wrote:

what is the best way to pass arguments to a mouse event?

well, what's "best" depends on your application and your personal taste.

making a function constructor is the best option but I have no real experience in that and i can't find anything on that kinda of code online.

self.ImagePlacer.Bind(wx.EVT_ENTER_WINDOW, self.OnOverImage)

Someone suggested using a lambda function but thats also something I'm pretty inexperienced with, any help would be great.

What is it you need to do?

Anyway, I've enclosed a little sample pf using lambda to pass an argument into an event callback.

-Chris

LambdaBind.py (722 Bytes)

···

--
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@noaa.gov

Chris,

I know nothing about lambda, have never used it yet. Could you explain a
bit about what's going on this code, particularly the point of it, here?:

btn.Bind(wx.EVT_BUTTON, lambda evt, temp=button_name: self.OnButton(evt, temp) )

I'm just having trouble understanding the mechanics of this.

Thanks,
Che

···

On Thu, Apr 9, 2009 at 6:41 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Dom wrote:

what is the best way to pass arguments to a mouse event?

well, what's "best" depends on your application and your personal taste.

making a function constructor is the best option but I have no real
experience in that and i can't find anything on that kinda of code online.

self.ImagePlacer.Bind(wx.EVT_ENTER_WINDOW, self.OnOverImage)

Someone suggested using a lambda function but thats also something I'm
pretty inexperienced with, any help would be great.

What is it you need to do?

Anyway, I've enclosed a little sample pf using lambda to pass an argument
into an event callback.

C M wrote:

I know nothing about lambda, have never used it yet. Could you explain a
bit about what's going on this code, particularly the point of it, here?:

googling for the python masters explanation may work better, but I'll try...

btn.Bind(wx.EVT_BUTTON, lambda evt, temp=button_name: self.OnButton(evt, temp) )

Bind() takes two (or more) arguments:

the event type: wx.EVT_BUTTON

and the callback. The callback should be a python callable that takes one argument -- the event object. In the usual case, that's easy, something like:

self.OnButton

where:

def OnButton(self, event):
    some_stuff

the self is the class instance object, so the event gets passed through as the only argument.

In this case, we are trying to pass some extra info into the callback: button_name. so we use a lamba:

lambda evt temp=button_name:

creates a function that takes two arguments, but only the first is required, the event, and the other has a default value. The way python keyword bindings work is that "temp" is bound to the OBJECT that is bound to button_name *When the function is created* -- that's key, 'cause this is in a loop, and that name is bound to a different object each time through the loop, and a new function object is created each time, each with a different object bound to "temp".

what the lambda function returns is return value of self.OnButton(evt, temp), with temp set to the button_name object.

so, the callback mechanism gets a callable that takes an event as an argument, and the OnButton method gets called with that event, and one other argument.

note that there is nothing special about lambda here -- it just lets you cram it all onto one line. You could do:

def callback(evt temp=button_name):
     return self.Onbutton(evt, temp)

btn.Bind(wx.EVT_BUTTON, callback )

and it would mean exactly the same thing.

HTH,

-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@noaa.gov

Robin Dunn wrote:

This is a common question, Chris could you make a quickie wiki page with your example and explanation?

I'll see if I can get to that today -- actually, when I wrote that example I intended it for a Wiki page, but I guess I never got to it.

Thanks for the prod.

-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@noaa.gov

Dear Chris, Robin & all,

The same question (passing arguments to an event handler) was in my mind and I tried a few things. One of them - I hope I’m not wrong - is simply using the name space that the instance shares with its methods, of which the handlers are part of.

I was working on it when Chris’s answer (using lambda) came in and I tried both. Would someone be kind enough to review my piece of code (very rough and simple) and see if it all make sense. In any cases, I see that it produces the expected result with any one of the two ways of doing.

The code is the short attached file.

Thank you in advance.

Wish you all a Happy and Holy Feast of Easter.

Enjoy this long week end. It seems Spring is coming (at least here in Belgium).

Rene

button.pyw (2.52 KB)

···

On Fri, Apr 10, 2009 at 5:56 PM, Robin Dunn robin@alldunn.com wrote:

Christopher Barker wrote:

C M wrote:

I know nothing about lambda, have never used it yet. Could you explain a
bit about what’s going on this code, particularly the point of it, here?:

googling for the python masters explanation may work better, but I’ll try…

This is a common question, Chris could you make a quickie wiki page with your example and explanation?


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


wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Christopher Barker wrote:

I'll see if I can get to that today --

Done:

http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

I've put a link to it in the FAQ -- I'm not sure if it should be linked to anywhere else.

It might be nice to expand that page, if anyone wants to contribute...

-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@noaa.gov

Christopher Barker wrote:

Christopher Barker wrote:

I'll see if I can get to that today --

Done:

Passing Arguments to Callbacks - wxPyWiki

I've put a link to it in the FAQ -- I'm not sure if it should be linked to anywhere else.

It might be nice to expand that page, if anyone wants to contribute...

-Chris

Hi Chris,
I added an example below yours to show using inline functions instead of lambdas. You did mention it, but I thought it would be worth showing a full example, hope that's okay

Great, looks good with the wiki formatting. There's just a couple
of typos... Should be a comma between evt and temp in two places,
right? (This had confused me at first, thinking there was some
special syntax to using the lambda).

Thanks again,
Che

···

On Fri, Apr 10, 2009 at 1:21 PM, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Christopher Barker wrote:

I'll see if I can get to that today --

Done:

Passing Arguments to Callbacks - wxPyWiki

I've put a link to it in the FAQ -- I'm not sure if it should be linked to
anywhere else.

I just made a couple of trivial changes - changed “create a function simple function” to “create a simple function”, and rearranged/ clarified a parenthetical in the first sentence after lambdas are introduced. Sorry - it’s the former English major in me…

Thanks for posting it - I sure wish it had been around when I was trying to figure this stuff out! Would’ve saved hours of headaches.
Here’s hoping it saves many more.

···

On Fri, Apr 10, 2009 at 10:56 AM, C M cmpython@gmail.com wrote:

On Fri, Apr 10, 2009 at 1:21 PM, Christopher Barker > > Chris.Barker@noaa.gov wrote:

Christopher Barker wrote:

I’ll see if I can get to that today –

Done:

http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

I’ve put a link to it in the FAQ – I’m not sure if it should be linked to

anywhere else.

Great, looks good with the wiki formatting. There’s just a couple

of typos… Should be a comma between evt and temp in two places,

right? (This had confused me at first, thinking there was some

special syntax to using the lambda).

Thanks again,

Che


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users


www.fsrtechnologies.com

Personally I like using the functools.partial function in this case. It's part of python's standard library from python 2.5 and up. Are you interested in me adding this way as an alternative to the wiki page?

-Matthias

···

Am 10.04.2009, 19:21 Uhr, schrieb Christopher Barker <Chris.Barker@noaa.gov>:

Christopher Barker wrote:

I'll see if I can get to that today --

Done:

Passing Arguments to Callbacks - wxPyWiki

I've put a link to it in the FAQ -- I'm not sure if it should be linked to anywhere else.

It might be nice to expand that page, if anyone wants to contribute...

C M wrote:

Great, looks good with the wiki formatting. There's just a couple
of typos... Should be a comma between evt and temp in two places,
right?

yup - I just fixed those.

Feel free to fix stuff like that yourself the next time, that's what Wikis are about.

Marc Tompkins wrote:

I just made a couple of trivial changes - changed "create a function simple function" to "create a simple function", and rearranged/ clarified a parenthetical in the first sentence after lambdas are introduced. Sorry - it's the former English major in me...

Nothing to be sorry for -- thanks! I had a meeting to get to, so I tried to whip that out so it would get done -- I appreciate any cleaning up anyone does.

Nitro wrote:

Personally I like using the functools.partial function in this case. It's part of python's standard library from python 2.5 and up. Are you interested in me adding this way as an alternative to the wiki page?

Sure -- I haven't done anything with partial yet, so I didn't put it in.

Maybe add a section heading or two to make it clear what's what.

-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@noaa.gov

Rene Heymans wrote:

The same question (passing arguments to an event handler) was in my mind and I tried a few things. One of them - I hope I'm not wrong - is simply using the name space that the instance shares with its methods, of which the handlers are part of.

That will work fine.

However, it won't work for the example I gave, because I was building the buttons in a loop. In the loop:

for button_name in ["first", "second", "third"]:
     ...
     btn.Bind(wx.EVT_BUTTON,
              lambda evt, temp=button_name: self.OnButton(evt, temp) )

I'm re-using the button_name variable. To do it like you did, you'd have to know which button name you wanted. You could do somethign like this:

self.name_dict = {}
for button_name in ["first", "second", "third"]:
     ...
     btn.Bind(wx.EVT_BUTTON, self.OnButton )
     self.name_dict[btn.Id] = button_name

then:

def OnButton(self, evt):
     name = self.name_dict[evt.GetEventId]
     ....

but I think the lambda trick is cleaner.

-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@noaa.gov

Done.

-Matthias

···

Am 10.04.2009, 22:11 Uhr, schrieb Christopher Barker <Chris.Barker@noaa.gov>:

Nitro wrote:

Personally I like using the functools.partial function in this case. It's part of python's standard library from python 2.5 and up. Are you interested in me adding this way as an alternative to the wiki page?

Sure -- I haven't done anything with partial yet, so I didn't put it in.

Maybe add a section heading or two to make it clear what's what.