How to pass an extra argument to a "Bind"ed function when binding it to widget?

Hi,

Suppose you have a wx.Choice widget, you bind a function to it:

self.my_choice = wx.Choice(self, -1, choices=[“a”,“b”])
self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

But I also want to pass an argument to “OnTypeChoice” function, when binding it,

def OnTypeChoice(self, evt, my_argument):
cb = evt.GetEventObject()
index = cb.GetCurrentSelection()
label = cb.GetString(index)
print "my_argument: ", my_argument

How should I change the Bind statement below to achieve this?

self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

self.Bind(wx.EVT_CHOICE, lambda event: self.OnTypeChoice(event,
"my_argument"), self.my_choice)

That should work

···

On 7 August 2014 16:48, steve <oslocourse@gmail.com> wrote:

Hi,

Suppose you have a wx.Choice widget, you bind a function to it:

self.my_choice = wx.Choice(self, -1, choices=["a","b"])
self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

But I also want to pass an argument to "OnTypeChoice" function, when binding
it,

def OnTypeChoice(self, evt, my_argument):
        cb = evt.GetEventObject()
        index = cb.GetCurrentSelection()
        label = cb.GetString(index)
        print "my_argument: ", my_argument

How should I change the Bind statement below to achieve this?

self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thank you.

···

On Thursday, August 7, 2014 7:12:55 PM UTC+3, Paul wrote:

self.Bind(wx.EVT_CHOICE, lambda event: self.OnTypeChoice(event,

“my_argument”), self.my_choice)

That should work

On 7 August 2014 16:48, steve osloc...@gmail.com wrote:

Hi,

Suppose you have a wx.Choice widget, you bind a function to it:

self.my_choice = wx.Choice(self, -1, choices=[“a”,“b”])

self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

But I also want to pass an argument to “OnTypeChoice” function, when binding

it,

def OnTypeChoice(self, evt, my_argument):

    cb = evt.GetEventObject()
    index = cb.GetCurrentSelection()
    label = cb.GetString(index)
    print "my_argument: ", my_argument

How should I change the Bind statement below to achieve this?

self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

You received this message because you are subscribed to the Google Groups

“wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an

email to wxpython-user...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

In addition to what Paul mentioned, see Passing Arguments to Callbacks - wxPyWiki. It has a couple of other interesting examples that you might find helpful.

  • Mike
···

On Thursday, August 7, 2014 10:48:14 AM UTC-5, steve wrote:

Hi,

Suppose you have a wx.Choice widget, you bind a function to it:

self.my_choice = wx.Choice(self, -1, choices=[“a”,“b”])
self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

But I also want to pass an argument to “OnTypeChoice” function, when binding it,

def OnTypeChoice(self, evt, my_argument):
cb = evt.GetEventObject()
index = cb.GetCurrentSelection()
label = cb.GetString(index)
print "my_argument: ", my_argument

How should I change the Bind statement below to achieve this?

self.Bind(wx.EVT_CHOICE, self.OnTypeChoice, self.my_choice )

Very cool, I need to read more on the lambda stuff!