box.columnconfigure(1, weight=1)
Button(text='Submit', command=onSubmit).grid(row=rownum, column=0, columnspan=2)
box.title('getfilegui-2')
box.bind('
mainloop()
This version makes a similar window (Figure 12-3), but adds a button at the bottom
that does the same thing as an Enter key press—it runs the getfile client procedure.
Generally speaking, importing and calling functions (as done here) is faster than run-
ning command lines, especially if done more than once. The getfile script is set up to
work either way—as program or function library.
Figure 12-3. getfilegui-2 in action
Using a reusable form-layout class
If you’re like me, though, writing all the GUI form layout code in those two scripts can
seem a bit tedious, whether you use packing or grids. In fact, it became so tedious to
me that I decided to write a general-purpose form-layout class, shown in Exam-
ple 12-20, which handles most of the GUI layout grunt work.
Example 12-20. PP4E\Internet\Sockets\form.py
"""
##################################################################
a reusable form class, used by getfilegui (and others)
##################################################################
"""
from tkinter import *
entrysize = 40
class Form: # add non-modal form box
def init(self, labels, parent=None): # pass field labels list
labelsize = max(len(x) for x in labels) + 2
box = Frame(parent) # box has rows, buttons
box.pack(expand=YES, fill=X) # rows has row frames
rows = Frame(box, bd=2, relief=GROOVE) # go=button or return key
rows.pack(side=TOP, expand=YES, fill=X) # runs onSubmit method
self.content = {}
846 | Chapter 12: Network Scripting