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

(yzsuai) #1

To see the effects generated by these two scripts’ settings, try out a few changes on your
computer. Most widgets can be given a custom appearance in the same way, and we’ll
see such options used repeatedly in this text. We’ll also meet operational configura-
tions, such as focus (for focusing input) and others. In fact, widgets can have dozens
of options; most have reasonable defaults that produce a native look-and-feel on each
windowing platform, and this is one reason for tkinter’s simplicity. But tkinter lets you
build more custom displays when you want to.


For more on ways to apply configuration options to provide common
look-and-feel for your widgets, refer back to “Customizing Widgets
with Classes” on page 400, especially its ThemedButton examples. Now
that you know more about configuration, its examples’ source code
should more readily show how configurations applied in widget sub-
classes are automatically inherited by all instances and subclasses. The
new ttk extension described in Chapter 7 also provides additional ways
to configure widgets with its notion of themes; see the preceding chapter
for more details and resources on ttk.

Top-Level Windows


tkinter GUIs always have an application root window, whether you get it by default or
create it explicitly by calling the Tk object constructor. This main root window is the
one that opens when your program runs, and it is where you generally pack your most
important and long-lived widgets. In addition, tkinter scripts can create any number
of independent windows, generated and popped up on demand, by creating Toplevel
widget objects.


Each Toplevel object created produces a new window on the display and automatically
adds it to the program’s GUI event-loop processing stream (you don’t need to call the
mainloop method of new windows to activate them). Example 8-3 builds a root and two
pop-up windows.


Figure 8-2. Config button at work


Top-Level Windows | 419
Free download pdf