python 2.7 - How can I prevent Tkinter from passing the event object in a binding? Is there any workaround to do this? -
i want bind method event using tkinter, don't need event object passed 'bind'-method. code clarity:
from tkinter import * root = tk() def callback(event): print 'clicked!' frame = frame(root, width=100, height=100) frame.bind('<button-1>', callback) frame.pack() root.mainloop()
here, argument event in callback unnecessary. there solution or workaround prevent bind-method passing event object?
what mean is, can call this:
def callback2(): print 'clicked!'
in binding? like:
frame.bind('<button-2>', callback2)
(which not working, because bin passes event, callback2 takes no arguments).
you have 3 broad options, see (in order of increasing complexity):
use ignored-by-convention variable name
_
:def callback2(_): ...
wrap callback when bind it:
frame.bind('...', lambda event: callback2())
write decorator ignore
event
parameter you:def no_event(func): @functools.wraps(func) def wrapper(event): return func() return wrapper
then apply callback methods don't need event:
@no_event def callback2(): ...
Comments
Post a Comment