Last but not least, we get to the code. PyMailGUI consists of the nine new modules listed at the start of this chapter; the source code for these modules is listed in this section.
Besides the code here, PyMailGUI also gets a lot of mileage out of reusing modules we wrote earlier and won’t repeat here:mailtoolsfor mail loads, composition, parsing, and delete operations;threadtoolsfor managing server and local file access threads; the GUI section’sTextEditorfor displaying and editing mail message text; and so on.
In addition, standard Python modules and packages such aspoplib,smtplib, andemail hide most of the details of pushing bytes around the Net and extracting and building message components. As usual, theTkinterstandard library module also implements GUI components in a portable fashion.
As mentioned earlier, PyMailGUI applies code factoring and OOP to leverage code reuse. For instance, list view windows are implemented as a common superclass that codes most actions, along with one subclass for the server inbox list window and one for local save-file list windows. The subclasses customize the common superclass for their specific mail media.
This design reflects the operation of the GUI itself—server list windows load mail over POP, and save-file list windows load from local files. The basic operation of 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 convention used for callback handler methods. Button 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 popups). PyMailGUI also includes a main module that defines startup window classes, a module that contains the help text as a string, and themailconfiguser settings module (a version specific to PyMailGUI is used here).
The next few sections list all of PyMailGUI’s code 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. PyMailGUI also includes a__init__.pyfile so that it can be used as a package—some of its modules may be useful in other programs. The __init__.py is empty in this package, so we omit it here.
Please check back next week for the continuation of this article.