[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

on Unix only, see other Tk and tkinter resources for more details on this topic. For
now, the next scheduled stop on this tour explores one of the more common uses of
top-level windows.


Dialogs


Dialogs are windows popped up by a script to provide or request additional informa-
tion. They come in two flavors, modal and nonmodal:


Modal
These dialogs block the rest of the interface until the dialog window is dismissed;
users must reply to the dialog before the program continues.


Nonmodal
These dialogs can remain on-screen indefinitely without interfering with other
windows in the interface; they can usually accept inputs at any time.


Regardless of their modality, dialogs are generally implemented with the Toplevel win-
dow object we met in the prior section, whether you make the Toplevel or not. There
are essentially three ways to present pop-up dialogs to users with tkinter—by using
common dialog calls, by using the now-dated Dialog object, and by creating custom
dialog windows with Toplevels and other kinds of widgets. Let’s explore the basics of
all three schemes.


Standard (Common) Dialogs


Because standard dialog calls are simpler, let’s start here first. tkinter comes with a
collection of precoded dialog windows that implement many of the most common pop
ups programs generate—file selection dialogs, error and warning pop ups, and question
and answer prompts. They are called standard dialogs (and sometimes common dia-
logs) because they are part of the tkinter library, and they use platform-specific library
calls to look like they should on each platform. A tkinter file open dialog, for instance,
looks like any other on Windows.


All standard dialog calls are modal (they don’t return until the dialog box is dismissed
by the user), and they block the program’s main window while they are displayed.
Scripts can customize these dialogs’ windows by passing message text, titles, and the
like. Since they are so simple to use, let’s jump right into Example 8-6 (coded as
a .pyw file here to avoid a shell pop up when clicked in Windows).


Example 8-6. PP4E\Gui\Tour\dlg1.pyw


from tkinter import
from tkinter.messagebox import


def callback():
if askyesno('Verify', 'Do you really want to quit?'):
showwarning('Yes', 'Quit not yet implemented')


426 | Chapter 8: A tkinter Tour, Part 1

Free download pdf