lebowski
lebowski

Reputation: 1051

How does canvas.bind(event, handler) pass event to the event handler?

The primary way of passing arguments to a function in Python is this:

def function(x):
    # function definition

function(y)

That is, when we call function we pass a value to it inside parenthesis.

However, I am using tkinter, and the event canvas.bind() method works thus:

def event_handler(event):
    # function definition

canvas.bind('event-name', event_handler)

That is, when canvas.bind calls the method event_handler, it seemingly does not pass the argument 'event-name' to event_handler as one would expect (i.e. by doing event_handler('event-name')). Instead, it just calls event_handler without any arguments.

So how does event-handler receive the event argument it is supposed to receive, going by its definition?

Upvotes: 0

Views: 100

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

It does it just like you expect. In the binding you are giving the name of the function to call. When the event fires, the internal tkinter code that processes the event actually calls it just as you would: event_handler(event).

Upvotes: 1

Related Questions