For more on input form layout, stay tuned for the form builder utilities
we’ll code near the end of Chapter 12 and use again in Chapter 13, when
developing a file transfer and FTP client user interface. As we’ll see,
doing forms well once allows us to skip the details later. We’ll also use
more custom form layout code in the PyEdit program’s change dialog
in Chapter 11, and the PyMailGUI example’s email header fields in
Chapter 14.
Laying Out Larger Tables with grid
So far, we’ve been building two-column arrays of labels and input fields. That’s typical
of input forms, but the tkinter grid manager is capable of configuring much grander
matrixes. For instance, Example 9-23 builds a five-row by four-column array of labels,
where each label simply displays its row and column number (row.col). When run, the
window in Figure 9-34 appears on-screen.
Example 9-23. PP4E\Gui\Tour\Grid\grid4.py
simple 2D table, in default Tk root window
from tkinter import *
for i in range(5):
for j in range(4):
lab = Label(text='%d.%d' % (i, j), relief=RIDGE)
lab.grid(row=i, column=j, sticky=NSEW)
mainloop()
Figure 9-34. A 5 × 4 array of coordinate labels
If you think this is starting to look like it might be a way to program spreadsheets, you
may be on to something. Example 9-24 takes this idea a bit further and adds a button
that prints the table’s current input field values to the stdout stream (usually, to the
console window).
574 | Chapter 9: A tkinter Tour, Part 2