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

(yzsuai) #1
We laid out input forms two ways in this section: by row frames with
fixed-width labels (entry2), and by column frames (entry3). In Chap-
ter 9 we’ll see a third form technique: layouts using the grid geometry
manager. Of these, gridding, and the rows with fixed-width labels of
entry2 tend to work best across all platforms.
Laying out by column frames as in entry3 works only on platforms
where the height of each label exactly matches the height of each entry
field. Because the two are not associated directly, they might not line up
properly on some platforms. When I tried running some forms that
looked fine on Windows XP on a Linux machine, labels and their cor-
responding entries did not line up horizontally.
Even the simple window produced by entry3 looks slightly askew on
closer inspection. It only appears the same as entry2 on some platforms
because of the small number of inputs and size defaults. On my Win-
dows 7 netbook, the labels and entries start to become horizontally
mismatched if you add 3 or 4 additional inputs to entry3’s fields tuple.
If you care about portability, lay out your forms either with the packed
row frames and fixed/maximum-width labels of entry2, or by gridding
widgets by row and column numbers instead of packing them. We’ll see
more on such forms in the next chapter. And in Chapter 12, we’ll write
a form-construction tool that hides the layout details from its clients
altogether (including its use case client in Chapter 13).

Checkbutton, Radiobutton, and Scale


This section introduces three widget types: the Checkbutton (a multiple-choice input
widget), the Radiobutton (a single-choice device), and the Scale (sometimes known as
a “slider”). All are variations on a theme and are somewhat related to simple buttons,
so we’ll explore them as a group here. To make these widgets more fun to play with,
we’ll reuse the dialogTable module shown in Example 8-8 to provide callbacks for
widget selections (callbacks pop up dialog boxes). Along the way, we’ll also use the
tkinter variables we just met to communicate with these widgets’ state settings.


Checkbuttons


The Checkbutton and Radiobutton widgets are designed to be associated with tkinter
variables: clicking the button changes the value of the variable, and setting the variable
changes the state of the button to which it is linked. In fact, tkinter variables are central
to the operation of these widgets:



  • A collection of Checkbuttons implements a multiple-choice interface by assigning
    each button a variable of its own.

  • A collection of Radiobuttons imposes a mutually exclusive single-choice model by
    giving each button a unique value and the same tkinter variable.


Checkbutton, Radiobutton, and Scale | 457
Free download pdf