Review Copy. Do not redistribute! 1999-12-01 22:15
Copyright (c) 1999 by Fredrik Lundh
19
Chapter 7. Events and Bindings
As was mentioned earlier, a Tkinter application spends most of its time inside an event loop
(entered via the
mainloop method). Events can come from various sources, including key
presses and mouse operations by the user, and redraw events from the window manager
(indirectly caused by the user, in many cases).
Tkinter provides a powerful mechanism to let you deal with events yourself. For each widget,
you can
bind Python functions and methods to events.
widget.bind(event, handler)
If an event matching the event description occurs in the widget, the given handler is called with
an object describing the event.
Here's a simple example:
Example 7-1. Capturing clicks in a window
# File: bind1.py
from Tkinter import *
root = Tk()
def callback(event):
print "clicked at", event.x, event.y
frame = Frame(root, width=100, height=100)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
In this example, we use the bind method of the frame widget to bind a callback function to an
event called
<Button-1>. Run this program and click in the window that appears. Each time
you click, a message like “
clicked at 44 63” is printed to the console window.
Events
Events are given as strings, using a special event syntax:
<modifier-type-detail>
The type field is the most important part of an event specifier. It specifies the kind of event that
we wish to bind, and can be user actions like
Button, and Key, or window manager events like
Enter, Configure, and others. The modifier and detail fields are used to give additional
information, and can in many cases be left out. There are also various ways to simplify the
Chapter 7. Events and Bindings
20
event string; for example, to match a keyboard key, you can leave out the angle brackets and
just use the key as is. Unless it is a space or an angle bracket, of course.
Instead of spending a few pages on discussing all the syntactic shortcuts, let's take a look on the
most common event formats:
Table 7-1. Event Formats
Event Description
<Button-1>
A mouse button is pressed over the widget. Button 1 is the leftmost
button, button 2 is the middle button (where available), and button 3 the
rightmost button. When you press down a mouse button over a widget,
Tkinter will automatically “grab” the mouse pointer, and mouse events
will then be sent to the current widget as long as the mouse button is held
down. The current position of the mouse pointer (relative to the widget)
is provided in the
x and y members of the event object passed to the
callback.
You can use
ButtonPress instead of Button, or even leave it out
completely:
<Button-1>, <ButtonPress-1>, and <1> are all synonyms.
For clarity, I prefer the
<Button-1> syntax.
<B1-Motion>
The mouse is moved, with mouse button 1 being held down (use B2 for
the middle button, B3 for the right button). The current position of the
mouse pointer is provided in the
x and y members of the event object
passed to the callback.
<Button-
Release-1>
Button 1 was released. The current position of the mouse pointer is
provided in the
x and y members of the event object passed to the
callback.
<Double-
Button-1>
Button 1 was double clicked. You can use Double or Triple as prefixes.
Note that if you bind to both a single click (<
Button-1>) and a double
click, both bindings will be called.
<Enter>
The mouse pointer entered the widget (this event doesn't mean that the
user pressed the Enter key!).
<Leave>
The mouse pointer left the widget.
<Return>
The user pressed the Enter key. You can bind to virtually all keys on the
keyboard. For an ordinary 102-key PC-style keyboard, the special keys
are Cancel (the Break key), BackSpace, Tab, Return(the Enter key),
Shift_L (any Shift key), Control_L (any Control key), Alt_L (any Alt
key), Pause, Caps_Lock, Escape, Prior (Page Up), Next (Page
Down), End, Home, Left, Up, Right, Down, Print, Insert, Delete,
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, Num_Lock, and
Scroll_Lock.
<Key>
The user pressed any key. The key is provided in the char member of the
event object passed to the callback (this is an empty string for special
keys).