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

(yzsuai) #1

The GUI interfaces PyTree utilizes were covered in depth earlier in this book. Because
it is written with Python and tkinter, it should be portable to Windows, Unix, and
Macs. At the top, it’s used with code of this form:


root = Tk() # build a single viewer GUI
bwrapper = BinaryTreeWrapper() # add extras: input line, test btns
pwrapper = ParseTreeWrapper() # make wrapper objects
viewer = TreeViewer(bwrapper, root) # start out in binary mode

def onRadio():
if var.get() == 'btree':
viewer.setTreeType(bwrapper) # change viewer's wrapper
elif var.get() == 'ptree':
viewer.setTreeType(pwrapper)

Figure 18-2 captures the display produced by PyTree under Python 3.1 on Windows 7
by running its top-level treeview.py file with no arguments; PyTree can also be started
from a button in the PyDemos launcher we met in Chapter 10. As usual, you can run
it on your own computer to get a better feel for its behavior. Although this screenshot
captures one specific kind of tree, PyTree is general enough to display arbitrary tree
types, and can even switch them while running.


Figure 18-2. PyTree viewing a binary search tree (test1 button)


PyTree: A Generic Tree Object Viewer| 1403
Free download pdf