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

(yzsuai) #1

Example 7-6. PP4E\Gui\Intro\gui1e.py—expansion


from tkinter import *
Label(text='Hello GUI world!').pack(expand=YES, fill=BOTH)
mainloop()


When widgets are packed, we can specify whether a widget should expand to take up
all available space, and if so, how it should stretch to fill that space. By default, widgets
are not expanded when their parent is. But in this script, the names YES and BOTH (im-
ported from the tkinter module) specify that the label should grow along with its parent,
the main window. It does so in Figure 7-3.


Figure 7-3. gui1e with widget resizing


Technically, the packer geometry manager assigns a size to each widget in a display
based on what it contains (text string lengths, etc.). By default, a widget can occupy
only its allocated space and is no bigger than its assigned size. The expand and fill
options let us be more specific about such things:


Figure 7-2. Expanding gui1


374 | Chapter 7: Graphical User Interfaces

Free download pdf