[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

if name == 'main':
root = Tk()
ents = makeform(root, fields)
root.bind('', (lambda event: fetch(ents)))
Button(root, text='Fetch',
command= (lambda: fetch(ents))).pack(side=LEFT)
Quitter(root).pack(side=RIGHT)
root.mainloop()


Figure 8-23. entry2 (and entry3) form displays


The input fields here are just simple Entry widgets. The script builds an explicit list of
these widgets to be used to fetch their values later. Every time you press this window’s
Fetch button, it grabs the current values in all the input fields and prints them to the
standard output stream:


C:\...\PP4E\Gui\Tour> python entry2.py
Input => "Bob"
Input => "Technical Writer"
Input => "Jack"

You get the same field dump if you press the Enter key anytime this window has the
focus on your screen; this event has been bound to the whole root window this time,
not to a single input field.


Most of the art in form layout has to do with arranging widgets in a hierarchy. This
script builds each label/entry row as a new Frame attached to the window’s current
TOP; fixed-width labels are attached to the LEFT of their row, and entries to the RIGHT.
Because each row is a distinct Frame, its contents are insulated from other packing going
on in this window. The script also arranges for just the entry fields to grow vertically
on a resize, as in Figure 8-24.


Going modal again


Later on this tour, we’ll see how to make similar form layouts with the grid geometry
manager, where we arrange by row and column numbers instead of frames. But now
that we have a handle on form layout, let’s see how to apply the modal dialog techniques
we met earlier to a more complex input display.


452 | Chapter 8: A tkinter Tour, Part 1

Free download pdf