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

(yzsuai) #1

###############################################################################


Self-test when file run standalone: 'python guimaker.py'


###############################################################################


if name == 'main':
from guimixin import GuiMixin # mix in a help method


menuBar = [
('File', 0,
[('Open', 0, lambda:0), # lambda:0 is a no-op
('Quit', 0, sys.exit)]), # use sys, no self here
('Edit', 0,
[('Cut', 0, lambda:0),
('Paste', 0, lambda:0)]) ]
toolBar = [('Quit', sys.exit, {'side': LEFT})]


class TestAppFrameMenu(GuiMixin, GuiMakerFrameMenu):
def start(self):
self.menuBar = menuBar
self.toolBar = toolBar


class TestAppWindowMenu(GuiMixin, GuiMakerWindowMenu):
def start(self):
self.menuBar = menuBar
self.toolBar = toolBar


class TestAppWindowMenuBasic(GuiMakerWindowMenu):
def start(self):
self.menuBar = menuBar
self.toolBar = toolBar # guimaker help, not guimixin


root = Tk()
TestAppFrameMenu(Toplevel())
TestAppWindowMenu(Toplevel())
TestAppWindowMenuBasic(root)
root.mainloop()


To make sense of this module, you have to be familiar with the menu fundamentals
introduced in Chapter 9. If you are, though, it’s straightforward—the GuiMaker class
simply traverses the menu and toolbar structures and builds menu and toolbar widgets
along the way. This module’s self-test code includes a simple example of the data
structures used to lay out menus and toolbars:


Menu bar templates
Lists and nested sublists of (label, underline, handler) triples. If a handler is a
sublist rather than a function or method, it is assumed to be a cascading submenu.


Toolbar templates
List of (label, handler, pack-options) triples. pack-options is coded as a dictionary
of options passed on to the widget pack method; we can code these as {'k':v}
literals, or with the dict(k=v) call’s keyword syntax. pack accepts a dictionary ar-
gument, but we could also transform the dictionary into individual keyword


606 | Chapter 10: GUI Coding Techniques

Free download pdf