Your bound function will already be called like callback(event)
from within tkinter's system, so your def
header takes one positional argument by default and is usually written def callback(event):
and bound with some_widget.bind(sequence, callback)
, just passing the function object to bind
and letting event
get passed along internally.
That having been said, there are two ways to use other variables from outside within event callbacks and still use the event
object too.
- Use
lambda
as a wrapper to pass along arbitrary args
:
a, b, c, d = some_bbox
def on_click(event, a, b, c, d):
print(event.x, event.y, a, b, c, d)
# do the rest of your processing
some_widget.bind("<Button-1>", lambda event, a=a, b=b, c=c, d=d: on_click(event, a, b, c, d)
- Use either the
global
or nonlocal
keyword to specify variables to take from the outer scope:
a, b, c, d = some_bbox
def on_click(event):
# use global if a, b, c, and d exist at the module level
global a, b, c, d
# use nonlocal if a, b, c, and d exist within the scope of another function
# nonlocal a, b, c, d
print(event.x, event.y, a, b, c, d)
# do the rest of your processing
some_widget.bind("<Button-1>", on_click)
python 3.8
tkinter 8.6