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

(yzsuai) #1

part.config(bd=2, relief=GROOVE) # or pass configs to Demo()
part.pack(side=LEFT, expand=YES, fill=BOTH) # grow, stretch with window
parts.append(part) # change list in-place


def dumpState():
for part in parts: # run demo report if any
print(part.module + ':', end=' ')
if hasattr(part, 'report'):
part.report()
else:
print('none')


root = Tk() # make explicit root first
root.title('Frames')
Label(root, text='Multiple Frame demo', bg='white').pack()
Button(root, text='States', command=dumpState).pack(fill=X)
Quitter(root).pack(fill=X)
addComponents(root)
root.mainloop()


Because all four demo launcher bars are coded as frames which attach themselves to
parent container widgets, this is easier than you might think: simply pass the same
parent widget (here, the root window) to all four demo constructor calls, and repack
and configure the demo objects as desired. Figure 8-32 shows this script’s graphical
result—a single window embedding instances of all four of the dialog demo launcher
demos we saw earlier. As coded, all four embedded demos grow and stretch with the
window when resized (try taking out the expand=YES to keep their sizes more constant).


Figure 8-32. demoAll_frm: nested subframes


Naturally, this example is artificial, but it illustrates the power of composition when
applied to building larger GUI displays. If you pretend that each of the four attached


472 | Chapter 8: A tkinter Tour, Part 1

Free download pdf