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

(yzsuai) #1
own text-based help window does similar work). For the break down by individual
files, see the Excel spreadsheet file linecounts.xls in the media subdirectory of PyMail-
GUI; this file is also used to test attachment sends and receives, and so appears near
the end of the emails in file SavedEmail\version30-4E if opened in the GUI (we’ll see
how to open mail save files in a moment).
Watch for the changes section ahead for size comparisons to prior versions. Also see
the SLOC counter script in Chapter 6 for an alternative way to count source lines that
is less manual, but can’t include all related files in a single run and doesn’t discriminate
between program code and help text.

Code Structure
As these statistics probably suggest, this is the largest example we’ll see in this book,
but you shouldn’t be deterred by its size. Because it uses modular and OOP techniques,
the code is simpler than you may think:


  • Python’s modules allow us to divide the system into files that have a cohesive
    purpose, with minimal coupling between them—code is easier to locate and un-
    derstand if your modules have a logical, self-contained structure.

  • Python’s OOP support allows us to factor code for reuse and avoid redundancy—
    as you’ll see, code is customized, not repeated, and the classes we will code reflect
    the actual components of the GUI to make them easy to follow.
    For instance, the implementation of mail list windows is easy to read and change, be-
    cause it has been factored into a common shared superclass, which is customized by
    subclasses for mail-server and save-file lists; since these are mostly just variations on a
    theme, most of the code appears in just one place. Similarly, the code that implements
    the message view window is a superclass shared by write, reply, and forward compo-
    sition windows; subclasses simply tailor it for writing rather than viewing.
    Although we’ll deploy these techniques in the context of a mail processing program
    here, such techniques will apply to any nontrivial program you’ll write in Python.
    To help get you started, the PyMailGuiHelp.py m o d u l e l i s t e d i n p a r t n e a r t h e e n d o f t h i s
    chapter includes a help text string that describes how this program is used, as well as
    its major features. You can also view this help live in both text and HTML form when
    the program is run. Experimenting with the system, while referring to its code, is prob-
    ably the best and quickest way to uncover its secrets.


Why PyMailGUI?


Before we start digging into the code of this relatively large system, some context is in
order. PyMailGUI is a Python program that implements a client-side email processing
user interface with the standard tkinter GUI toolkit. It is presented both as an instance

1008 | Chapter 14: The PyMailGUI Client

Do


wnload from Wow! eBook <www.wowebook.com>

Free download pdf