script method calls and window closures in general, including those at program exit.
If you bind this on a window, it will be triggered once for each widget in the window;
the callback’s event argument widget attribute gives the widget being destroyed, and
you can check this to detect a particular widget’s destruction. If you bind this on a
specific widget instead, it will be triggered once for that widget’s destruction only.
It’s important to know that a widget is in a “half dead” state (Tk’s terminology) when
this event is triggered—it still exists, but most operations on it fail. Because of that, the
widget’s changed state or fetching its content in a
exceptions. In addition, this event’s handler cannot cancel the destruction in general
and resume the GUI; if you wish to intercept and verify or suppress window closes
when a user clicks on a window’s X button, use WM_DELETE_WINDOW in top-level windows’
protocol methods as described earlier in this chapter.
You should also know that running a tkinter widget’s quit method does not trigger any
if any
event for non-GUI window exit actions should usually call destroy instead of quit to
close, and rely on the fact that a program exits when the last remaining or only Tk root
window (default or explicit) is destroyed as described earlier. This precludes using
quit for immediate shutdowns, though you can still run sys.exit for brute-force exits.
A script can also perform program exit actions in code run after the mainloop call re-
turns, but the GUI is gone completely at this point, and this code is not associated with
any particular widget. Watch for more on this event when we study the PyEdit example
program in Chapter 11; at the risk of spoiling the end of this story, we’ll find it unusable
for verifying changed text saves.
Message and Entry
The Message and Entry widgets allow for display and input of simple text. Both are
essentially functional subsets of the Text widget we’ll meet later; Text can do everything
Message and Entry can, but not vice versa.
Message
The Message widget is simply a place to display text. Although the standard showinfo
dialog we met earlier is perhaps a better way to display pop-up messages, Message splits
up long strings automatically and flexibly and can be embedded inside container widg-
ets any time you need to add some read-only text to a display. Moreover, this widget
sports more than a dozen configuration options that let you customize its appearance.
Example 8-16 and Figure 8-21 illustrate Message basics, and demonstrates how
Message reacts to horizontal stretching with fill and expand; see Chapter 7 for more
on resizing and Tk or tkinter references for other options Message supports.
448 | Chapter 8: A tkinter Tour, Part 1