How to pass an argument to event handler in tkinter?

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)

Leave a Comment