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

(yzsuai) #1

use on Windows, there is also a one-line .pyw file that just executes the .py file’s contents
with an execfile('textEditor.py') call. The .pyw suffix avoids the DOS console
streams window pop up when launched by clicking on Windows.


Today, .pyw files can be both imported and run, like normal .py files (they can also be
double-clicked, and launched by Python tools such as os.system and os.startfile), so
we don’t really need a separate file to support both import and console-less run modes.
I retained the .py, though, in order to see printed text during development and to use
PyEdit as a simple IDE—when the run code option is selected, in nonfile mode, printed
output from code being edited shows up in PyEdit’s DOS console window in Windows.
Clients will normally import the .py file.


User configurations file


On to the code. First, PyEdit’s user configuration module is listed in Example 11-1.
This is mostly a convenience, for providing an initial look-and-feel other than the de-
fault. PyEdit is coded to work even if this module is missing or contains syntax errors.
This file is primarily intended for when PyEdit is the top-level script run (in which case
the file is imported from the current directory), but you can also define your own version
of this file elsewhere on your module import search path to customize PyEdit.


See textEditor.py ahead for more on how this module’s settings are loaded. Its contents
are loaded by two different imports—one import for cosmetic settings assumes this
module itself (not its package) is on the module search path and skips it if not found,
and the other import for Unicode settings always locates this file regardless of launch
modes. Here’s what this division of configuration labor means for clients:



  • Because the first import for cosmetic settings is relative to the module search path,
    not to the main file’s package, a new textConfig.py can be defined in each client
    application’s home directory to customize PyEdit windows per client.

  • Conversely, Unicode settings here are always loaded from this file using package
    relative imports if needed, because they are more critical and unlikely to vary. The
    package relative import used for this is equivalent to a full package import from
    the PP4E root, but not dependent upon directory structure.


Like much of the heuristic Unicode interface described earlier, this import model is
somewhat preliminary, and may require revision if actual usage patterns warrant.


Example 11-1. PP4E\Gui\TextEditor\textConfig.py


"""
PyEdit (textEditor.py) user startup configuration module;
"""


#----------------------------------------------------------------------------------


General configurations


comment-out any setting in this section to accept Tk or program defaults;


can also change font/colors from GUI menus, and resize window when open;


imported via search path: can define per client app, skipped if not on the path;


694 | Chapter 11: Complete GUI Programs

Free download pdf