default = 0, strings = ('spam', 'SPAM'))
if name == 'main': OldDialogDemo().mainloop()
If you supply Dialog a tuple of button labels and a message, you get back the index of
the button pressed (the leftmost is index zero). Dialog windows are modal: the rest of
the application’s windows are disabled until the Dialog receives a response from the
user. When you press the Pop2 button in the main window created by this script, the
second dialog pops up, as shown in Figure 8-17.
Figure 8-17. Old-style dialog
This is running on Windows, and as you can see, it is nothing like what you would
expect on that platform for a question dialog. In fact, this dialog generates an X Win-
dows look-and-feel, regardless of the underlying platform. Because of both Dialog’s
appearance and the extra complexity required to program it, you are probably better
off using the standard dialog calls of the prior section instead.
Custom Dialogs
The dialogs we’ve seen so far have a standard appearance and interaction. They are fine
for many purposes, but often we need something a bit more custom. For example,
forms that request multiple field inputs (e.g., name, age, shoe size) aren’t directly ad-
dressed by the common dialog library. We could pop up one single-input dialog in turn
for each requested field, but that isn’t exactly user friendly.
Custom dialogs support arbitrary interfaces, but they are also the most complicated to
program. Even so, there’s not much to it—simply create a pop-up window as a
Toplevel with attached widgets, and arrange a callback handler to fetch user inputs
entered in the dialog (if any) and to destroy the window. To make such a custom dialog
modal, we also need to wait for a reply by giving the window input focus, making other
windows inactive, and waiting for an event. Example 8-13 illustrates the basics.
Example 8-13. PP4E\Gui\Tour\dlg-custom.py
import sys
from tkinter import *
makemodal = (len(sys.argv) > 1)
Dialogs | 439