Events in tkinter are all the command inputs that can happen inside your application such as mouse movements / clicks, keyboard entries, window resizes, widget introduction, the list goes on. Generally there are two types of events, those which are built into a widget and those which are explicitly bound to a widget.

Implicit Events

Some widgets have events built into them such as the Button widget which takes a command= argument. When we click the button, the callable in our command parameter will be called. Some other widgets with built in event bindings (via command=) are:

Explicitly-Bound Events

There are many events which can be bound to any widget with the syntax: widget.bind("<Event-Specifier>", callable_handler). This causes the specified handler to get called whenever the event occurs. Note that we can also bind events to our container objects such as Tk, Frame, & TopLevel

Binding Options

There are multiple ways to bind events to widgets in tkinter:

Be aware that .bind_class() can get in the way of default widget behavior since this is how tkinter itself implements default behaviors for widgets. The preferred method is creating a widget class that inherits from the base class and configures a bind on instantiation.

class BoundButton(tkinter.Button):
def __init__(self, master, **kw):
  apply(tkinter.Button.__init__, (self, master), kw)
  self.bind("<Return>", lambda event: print(event))

Declaring Callables

When we declare callables to act as event handlers we should take note that the triggering event will pass an event object (see below) into our callable function. This means we should define our event handlers with one parameter. Implicit events sometimes have the additional argument.

def keyboard_handler(event):

def mouse_handler(event):
print(event.x, event.y)
print(event.char) # prints ??

Note that we can also pass callables directly into the .bind() function using lambdas.

root.bind("k", lambda event: print("k was pressed!"))

The Event Object

The event object passed into our event-handlers has many attributes. Some of the commonly used ones are: