Home arrow Python arrow A PyMailGUI Demo

A PyMailGUI Demo

In last week's article, we introduced the PyMailGUI client, part of a Python program that sends, receives, composes, and parses Internet email messages. This week, we'll begin walking you through a PyMailGUI demo. This article, the second of six parts, is excerpted from chapter 15 of the book Programming Python, Third Edition, written by Mark Lutz (O'Reilly, 2006; ISBN: 0596009259). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. A PyMailGUI Demo
  2. Loading Mail
  3. Threading Model
  4. Load Server Interface
By: O'Reilly Media
Rating: starstarstarstarstar / 1
July 19, 2007

print this article




A PyMailGUI Demo

PyMailGUI is a multiwindow interface. It consists of the following:

  1. A main mail-server list window opened initially, for online mail processing
  2. One or more mail save-file list windows for offline mail processing
  3. One or more mail-view windows for viewing and editing messages
  4. Text editor windows for displaying the system’s source code
  5. Nonblocking busy state pop-up dialogs
  6. Assorted pop-up dialogs for opened message parts, help, and more

Operationally, PyMailGUI runs as a set of parallel threads, which may overlap in time: one for each active server transfer, and one for each active offline save file load or deletion. PyMailGUI supports mail save files, automatic saves of sent messages, configurable fonts and colors, viewing and adding attachments, main message text extraction, and much more.

To make this case study easier to understand, let’s begin by seeing what PyMailGUI actually does—its user interaction and email processing functionality—before jumping into the Python code that implements that behavior. As you read this part, feel free to jump ahead to the code listings that appear after the screenshots, but be sure to read this section too; this is where some subtleties of PyMailGUI’s design are explained. After this section, you are invited to study the system’s Python source code listings on your own for a better and more complete explanation than can be crafted in English.

Getting Started

PyMailGUI is a Python/Tkinter program, run by executing its top-level script file, PyMailGui.py. Like other Python programs, PyMailGUI can be started from the system command line by clicking on its filename icon in a file explorer interface, or by pressing its button in the PyDemos or PyGadgets launcher bar. However it is started, the first window PyMailGUI presents is shown in Figure 15-1. Notice the “PY” window icon: this is the handiwork of window protocol tools we wrote earlier in this book.

This is the PyMailGUI main window—every operation starts here. It consists of:

  1. A help button (the bar at the top)
  2. A clickable email list area for fetched emails (the middle section)
  3. A button bar at the bottom for processing messages selected in the list area


Figure 15-1.  PyMailGUI main server list window

In normal operation, users load their email, select an email from the list area by clicking on it, and press a button at the bottom to process it. No mail messages are shown initially; we need to first load them with the Load button—a simple password input dialog is displayed, a busy dialog appears that counts down message headers being loaded to give a status indication, and the index is filled with messages ready to be selected.

PyMailGUI’s list windows, such as the one in Figure 15-1, display mail header details in fixed-width columns, up to a maximum size. Mails with attachments are prefixed with a “*” in mail index list windows, and fonts and colors in PyMailGUI windows may be customized by the user in themailconfigconfiguration file. You can’t tell in this black-and white book, but by default, mail index lists are Indian red, view windows are a shade of purple, pop-up PyEdit windows are light cyan, and help is steel blue; you can change most of these as you like (see Example 9-11 for help with color definition strings).

List windows allow multiple messages to be selected at once—the action selected at the bottom of the window is applied to all selected mails. For instance, to view many mails, select them all and press View; each will be fetched and displayed in its own view window. Use Ctrl-Click and Shift-Click to select more than one (the standard Windows multiple selection operations apply—try it).

Before we go any further, though, let’s press the help bar at the top of the list window in Figure 15-1 to see what sort of help is available; Figure 15-2 shows the help window popup that appears.

The main part of this window is simply a block of text in a scrolled-text widget, along with two buttons at the bottom. The entire help text is coded as a single triple-quoted string in the Python program. We could get fancier and spawn a web browser

Figure 15-2.  PyMailGUI help popup

to view HTML-formatted help, but simple text does the job here.* The Cancel button makes this nonmodal (i.e., nonblocking) window go away; more interestingly, the Source button pops up PyEdit text editor viewer windows for all the source files of PyMailGUI’s implementation; Figure 15-3 captures one of these (there are many—this is intended as a demonstration, not as a development environment). Not every program shows you its source code, but PyMailGUI follows Python’s open source motif.

When a message is selected in the mail list window, PyMailGUI downloads its full text (if it has not yet been downloaded in this session), and an email viewer window appears, as captured in Figure 15-4. View windows are built in response to actions in list windows; this is described next.

Figure 15-3.  PyMailGUI source code viewer window

  • The top portion consists of action buttons (“Parts” to list all message parts, “Split” to save and open parts using a selected directory, and “Cancel” to remove this nonmodal window) along with a section for displaying email header lines (“From:”, “To:”, and so on).
  • In the middle, a row of quick-access buttons for opening message parts, including attachments, appears. When clicked, PyMailGUI opens known and generally safe parts according to their type (media types may open in a web browser, text parts in PyEdit, Windows document types per the Windows Registry, and so on).
  • The bulk of this window is just another reuse of theTextEditorclass object we wrote in Chapter 12 for the PyEdit program—PyMailGUI simply attaches an instance ofTextEditor to every view and compose window in order to get a full-featured text editor component for free. In fact, much on this window is implemented byTextEditor, not by PyMailGUI.

For instance, if we pick the Tools menu of the text portion of this window and select its Info entry, we get the standard PyEditTextEditorobject’s file text statistics box—the same popup we’d get in the standalone PyEdit text editor and in the PyView image view programs we wrote in Chapter 12 (see Figure 15-5).

In fact, this is the third reuse ofTextEditorin this book: PyEdit, PyView, and now PyMailGUI all present the same text-editing interface to users, simply because they all use the sameTextEditorobject and code. PyMailGUI both attaches instances of this class for mail viewing and editing, and pops up instances for source-code viewing. For mail views, PyMailGUI customizes text fonts and colors per its own configuration module.

Figure 15-4.  PyMailGUI view window

To display email, PyMailGUI inserts its text into an attachedTextEditor object; to compose email, PyMailGUI presents aTextEditorand later fetches all its text to ship over the Net. Besides the obvious simplification here, this code reuse makes it easy to pick up improvements and fixes—any changes in theTextEditorobject are automatically inherited by PyMailGUI, PyView, and PyEdit. In the current version, for instance, PyMailGUI supports edit undo and redo, just because PyEdit now does, too.

>>> More Python Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: