I would like to pass both event and an additional argument to a handler function.
The purpose of this is to display a context menu using the x_root
and y_root
arguments of the event object.
I have tried the two techniques shown below. For simplicity, I prefer the one that uses the lambda function, but it doesn't work.
Is it possible to fix it? Any idea or explanation?
import tkinter as tk
class Example:
def __init__(self, window):
base = tk.Frame(window).pack()
for i in range(3):
l = tk.Label(base, text='label '+str(i))
l.pack()
def interhandler(event, self=self, i=i):
return self._handler_one(event, i)
l.bind("<Button-1>", interhandler)
l.bind("<Button-3>", lambda event, i=i: self._handler_two(i))
def _handler_one(self, event, k):
print(event) # works
print('left click on label', k)
def _handler_two(self, k):
print(event) # does not work
print('right click on label ', k)
main = tk.Tk()
example = Example(main)
main.mainloop()