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

(yzsuai) #1

higher-level functionality that we might otherwise have to code redundantly. Among
other things, the classes arrange for automatic quit verification dialog pop ups and icon
file searching. For instance, the window classes always search the current working
directory and the directory containing this module for a window icon file, once per
process.


By using classes that encapsulate—that is, hide—such details, we inherit powerful tools
without having to think about their implementation again in the future. Moreover, by
using such classes, we’ll give our applications a standard look-and-feel and behavior.
And if we ever need to change that appearance or behavior, we have to change code in
only one place, not in every window we implement.


To test this utility module, Example 10-17 exercises its classes in a variety of modes—
as mix-in classes, as superclasses, and as calls from nonclass code.


Example 10-17. PP4E\Gui\Tools\windows-test.py


must import windows to test, else name is main in findIcon


from tkinter import Button, mainloop
from windows import MainWindow, PopupWindow, ComponentWindow


def _selftest():


mixin usage


class content:
"same code used as a Tk, Toplevel, and Frame"
def init(self):
Button(self, text='Larch', command=self.quit).pack()
Button(self, text='Sing ', command=self.destroy).pack()


class contentmix(MainWindow, content):
def init(self):
MainWindow.init(self, 'mixin', 'Main')
content.init(self)
contentmix()


class contentmix(PopupWindow, content):
def init(self):
PopupWindow.init(self, 'mixin', 'Popup')
content.init(self)
prev = contentmix()


class contentmix(ComponentWindow, content):
def init(self): # nested frame
ComponentWindow.init(self, prev) # on prior window
content.init(self) # Sing erases frame
contentmix()


subclass usage


class contentsub(PopupWindow):
def init(self):
PopupWindow.init(self, 'popup', 'subclass')


Wrapping Up Top-Level Window Interfaces | 633
Free download pdf