widget.bind('<Button-1>',callback) # binding def callback(self,event) #do something
I need to pass an argument to callback()
. The argument is a dictionary object.
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.
To bind the <Enter> key with an event in Tkinter window, we can use bind('<Return>', callback) by specifying the key and the callback function as the arguments. Once we bind the key to an event, we can get full control over the events.
Python Tkinter event handler It is simply an action like clicking on a button that leads to another event. Code: In the following code, we define a class in which we created an object. We added an event handler on a button which is actions to quit a window. It Handles the input received in an event.
Use the bind() method to bind an event to a widget. Tkinter supports both instance-level and class-level bindings.
You can use lambda
to define an anonymous function, such as:
data={"one": 1, "two": 2} widget.bind("<ButtonPress-1>", lambda event, arg=data: self.on_mouse_down(event, arg))
Note that the arg
passed in becomes just a normal argument that you use just like all other arguments:
def on_mouse_down(self, event, arg): print(arg)
What about
import functools def callback(self, event, param): pass arg = 123 widget.bind("", functools.partial(callback, param=arg))
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