space. With grid, the sticky option serves the roles of both fill and anchor in the
packer. Gridded widgets can optionally be made sticky on one side of their allo-
cated cell space (such as anchor) or on more than one side to make them stretch
(such as fill). Widgets can be made sticky in four directions—N, S, E, and W, and
concatenations of these letters specify multiple-side stickiness. For instance, a
sticky setting of W left justifies the widget in its allocated space (such as a packer
anchor=W), and NS stretches the widget vertically within its allocated space (such as
a packer fill=Y).
Widget stickiness hasn’t been useful in examples thus far because the layouts were
regularly sized (widgets were no smaller than their allocated grid cell space), and
resizes weren’t supported at all. Here, though, Example 9-22 specifies NSEW stick-
iness to make widgets stretch in all directions with their allocated cells.
Different combinations of row and column weights and sticky settings generate differ-
ent resize effects. For instance, deleting the columnconfig lines in the grid3 script makes
the display expand vertically but not horizontally. Try changing some of these settings
yourself to see the sorts of effects they produce.
Spanning columns and rows
There is one other big difference in how the grid3 script configures its windows. Both
the grid and the pack windows display a label on the top that spans the entire window.
For the packer scheme, we simply make a label attached to the top of the window at
large (remember, side defaults to TOP):
Label(root, text='Pack').pack()
Because this label is attached to the window’s top before any row frames are, it appears
across the entire window top as expected. But laying out such a label takes a bit more
work in the rigid world of grids; the first line of the grid implementation function does
it like this:
Label(root, text='Grid').grid(columnspan=2)
To make a widget span across multiple columns, we pass grid a columnspan option with
a spanned-column count. Here, it just specifies that the label at the top of the window
should stretch over the entire window—across both the label and the entry columns.
To make a widget span across multiple rows, pass a rowspan option instead. The regular
layouts of grids can be either an asset or a liability, depending on how regular your user
interface will be; these two span settings let you specify exceptions to the rule when
needed.
So which geometry manager comes out on top here? When resizing is factored in, as
in the script in Example 9-22, gridding actually becomes slightly more complex; in fact,
gridding requires three extra lines of code. On the other hand, enumerate could again
make the race close, grid is still convenient for simple forms, and your grids and packs
may vary.
Grids | 573