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

(yzsuai) #1
list window layout and actions, though, is similar for both and is shared in the
common superclass to avoid redundancy and simplify the code. Message view
windows are similarly factored: a common view window superclass is reused and
customized for write, reply, and forward view windows.
To make the code easier to follow, it is divided into two main modules that reflect
the structure of the GUI—one for the implementation of list window actions and
one for view window actions. If you are looking for the implementation of a button
that appears in a mail view or edit window, for instance, see the view window
module and search for a method whose name begins with the word on—the con-
vention used for callback handler methods. A specific button’s text can also be
located in name/callback tables used to build the windows. Actions initiated on
list windows are coded in the list window module instead.
In addition, the message cache is split off into an object and module of its own,
and potentially reusable tools are coded in importable modules (e.g., line wrapping
and utility pop ups). PyMailGUI also includes a main module that defines startup
window classes, a simple HTML to plain-text parser, a module that contains the
help text as a string, the mailconfig user settings module (a version specific to
PyMailGUI is used here), and a small handful of related files.

The following subsections present each of PyMailGUI’s source code files for you to
study. As you read, refer back to the demo earlier in this chapter and run the program
live to map its behavior back to its code.


One accounting note up-front: the only one of PyMailGUI’s 18 new source files not
listed in this section is its init.py package initialization file. This file is mostly
empty except for a comment string and is unused in the system today. It exists only for
future expansion, in case PyMailGUI is ever used as a package in the future—some of
its modules may be useful in other programs. As is, though, same-directory internal
imports here are not package-relative, so they assume this system is either run as a top-
level program (to import from “.”) or is listed on sys.path directly (to use absolute
imports). In Python 3.X, a package’s directory is not included on sys.path automati-
cally, so future use as a package would require changes to internal imports (e.g., moving
the main script up one level and using from. import module throughout). See resources
such as the book Learning Python for more on packages and package imports.


PyMailGUI: The Main Module


Example 14-1 defines the file run to start PyMailGUI. It implements top-level list win-
dows in the system—combinations of PyMailGUI’s application logic and the window
protocol superclasses we wrote earlier in the text. The latter of these define window
titles, icons, and close behavior.


The main internal, nonuser documentation is also in this module, as well as command-
line logic—the program accepts the names of one or more save-mail files on the


PyMailGUI Implementation| 1063
Free download pdf