Is it possible to pass additional arguments to an event callback?
For example, if my event binding looked like this;
self.Bind(wx.EVT_BUTTON, self.do_something, self.button)
How could I pass the arguments to my method?
self.do_something(self,event,arguments):
"""do something with arguments"""
pass
Of course you can. (If it was the event you define yourself, you would need to create you own event arguments class where you would put all the information you need.
If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.
Callback Functions A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function, which contains the logic for when the callback function gets executed.
Use functools.partial
, or in the general case a lambda
expression.
The partial form would be
functools.partial(self.do_something, args)
Note that in this case the event
argument will be passed at the end of the argument list. The equivalent lambda form is:
lambda event: self.do_something(args, event)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With