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

(yzsuai) #1

Because the programs used and their behavior may vary widely from machine to ma-
chine, though, you’re best off studying this script’s code and running it on your own
computer and with your own test files to see what happens. As usual, you can also test
it interactively (use the package path like this one to import from a different directory,
assuming your module search path includes the PP4E root):


>>> from PP4E.System.Media.playfile import playfile
>>> playfile(r'C:\movies\mov10428.mpg') # video/mpeg

We’ll use the playfile module again as an imported library like this in Chapter 13 to
open media files downloaded by FTP. Again, you may want to tweak this script’s tables
for your players. This script also assumes the media file is located on the local machine
(even though the webbrowser module supports remote files with “http://” names), and
it does not currently allow different players for most different MIME subtypes (it spe-
cial-cases text to handle “plain” and “html” differently, but no others). In fact, this
script is really just something of a simple framework that was designed to be extended.
As always, hack on; this is Python, after all.


Automated Program Launchers (External)


Finally, some optional reading—in the examples distribution package for this book
(available at sites listed in the Preface) you can find additional system-related scripts
we do not have space to cover here:



  • PP4E\Launcher.py—contains tools used by some GUI programs later in the book
    to start Python programs without any environment configuration. Roughly, it sets
    up both the system path and module import search paths as needed to run book
    examples, which are inherited by spawned programs. By using this module to
    search for files and configure environments automatically, users can avoid (or at
    least postpone) having to learn the intricacies of manual environment configura-
    tion before running programs. Though there is not much new in this example from
    a system interfaces perspective, we’ll refer back to it later, when we explore GUI
    programs that use its tools, as well as those of its launchmodes cousin, which we
    wrote in Chapter 5.

  • PP4E\Launch_PyDemos.pyw and PP4E\Launch_PyGadgets_bar.pyw—use
    Launcher.py to start major GUI book examples without any environment config-
    uration. Because all spawned processes inherit configurations performed by the
    launcher, they all run with proper search path settings. When run directly, the
    underlying PyDemos2.pyw and PyGadgets_bar.pyw scripts (which we’ll explore
    briefly at the end of Chapter 10) instead rely on the configuration settings on the
    underlying machine. In other words, Launcher effectively hides configuration de-
    tails from the GUI interfaces by enclosing them in a configuration program layer.

  • PP4E\LaunchBrowser.pyw—portably locates and starts an Internet web browser
    program on the host machine in order to view a local file or remote web page. In


Automated Program Launchers (External) | 351
Free download pdf