ent.grid(row=row, column=1)
ent.insert(0, 'grid')
We’ll leave further code compaction to the more serious sports fans in the audience
(this code isn’t too horrific, but making your code concise in general is not always in
your coworkers’ best interest!). Irrespective of coding tricks, the complexity of packing
and gridding here seems similar. As we’ll see later, though, gridding can require more
code when widget resizing is factored into the mix.
Combining grid and pack
Notice that the prior section’s Example 9-19 passes a brand-new Toplevel to each form
constructor function so that the grid and pack versions wind up in distinct top-level
windows. Because the two geometry managers are mutually exclusive within a given
parent container, we have to be careful not to mix them improperly. For instance,
Example 9-20 is able to put both the packed and the gridded widgets on the same
window, but only by isolating each in its own Frame container widget.
Example 9-20. PP4E\Gui\Tour\Grid\grid2-same.py
"""
build pack and grid forms on different frames in same window;
can't grid and pack in same parent container (e.g., root window)
but can mix in same window if done in different parent frames;
"""
from tkinter import *
from grid2 import gridbox, packbox
root = Tk()
Label(root, text='Grid:').pack()
Figure 9-30. Equivalent grid and pack windows
568 | Chapter 9: A tkinter Tour, Part 2