Button(text='Clear', command=onClear).grid(row=numrow+1, column=2)
Button(text='Quit', command=sys.exit).grid(row=numrow+1, column=3)
mainloop()
Figure 9-36 shows this script at work summing up four columns of numbers; to get a
different-size table, change the numrow and numcol variables at the top of the script.
Figure 9-36. Adding column sums
And finally, Example 9-26 is one last extension that is coded as a class for reusability,
and it adds a button to load the table’s data from a file. Data files are assumed to be
coded as one line per row, with whitespace (spaces or tabs) between each column within
a row line. Loading a file of data automatically resizes the table GUI to accommodate
the number of columns in the table based upon the file’s content.
Example 9-26. PP4E\Gui\Tour\Grid\grid5c.py
recode as an embeddable class
from tkinter import *
from tkinter.filedialog import askopenfilename
from PP4E.Gui.Tour.quitter import Quitter # reuse, pack, and grid
class SumGrid(Frame):
def init(self, parent=None, numrow=5, numcol=5):
Frame.init(self, parent)
self.numrow = numrow # I am a frame container
self.numcol = numcol # caller packs or grids me
self.makeWidgets(numrow, numcol) # else only usable one way
def makeWidgets(self, numrow, numcol):
self.rows = []
for i in range(numrow):
cols = []
for j in range(numcol):
ent = Entry(self, relief=RIDGE)
ent.grid(row=i+1, column=j, sticky=NSEW)
ent.insert(END, '%d.%d' % (i, j))
cols.append(ent)
self.rows.append(cols)
Grids | 577