C:\...\PP4E\Gui\Intro> python gui3.py
Hello, I must be going...
C:\...\PP4E\Gui\Intro> python gui3b.py
Hello lambda world
C:\...\PP4E\Gui\Intro> python gui3c.py
Hello class method world
C:\...\PP4E\Gui\Intro> python gui3d.py
Hello __call__ world
There are good reasons for each callback coding scheme (function, lambda, class
method, callable class), but we need to move on to larger examples in order to uncover
them in less theoretical terms.
Other tkinter Callback Protocols
For future reference, also keep in mind that using command options to intercept user-
generated button press events is just one way to register callbacks in tkinter. In fact,
there are a variety of ways for tkinter scripts to catch events:
Button command options
As we’ve just seen, button press events are intercepted by providing a callable object
in widget command options. This is true of other kinds of button-like widgets we’ll
meet in Chapter 8 (e.g., radio and check buttons and scales).
Menu command options
In the upcoming tkinter tour chapters, we’ll also find that a command option is used
to specify callback handlers for menu selections.
Scroll bar protocols
Scroll bar widgets register handlers with command options, too, but they have a
unique event protocol that allows them to be cross-linked with the widget they are
meant to scroll (e.g., listboxes, text displays, and canvases): moving the scroll bar
automatically moves the widget, and vice versa.
General widget bind methods
A more general tkinter event bind method mechanism can be used to register call-
back handlers for lower-level interface events—key presses, mouse movement and
clicks, and so on. Unlike command callbacks, bind callbacks receive an event object
argument (an instance of the tkinter Event class) that gives context about the
event—subject widget, screen coordinates, and so on.
Window manager protocols
In addition, scripts can also intercept window manager events (e.g., window close
requests) by tapping into the window manager protocol method mechanism avail-
able on top-level window objects. Setting a handler for WM_DELETE_WINDOW, for in-
stance, takes over window close buttons.
Adding User-Defined Callback Handlers | 393