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

(yzsuai) #1

As pictured in Figure 10-17, PyDemos also constructs two pop-up windows when but-
tons at the bottom of the main window are pressed—an Info pop up giving a short
description of the last demo spawned, and a Links pop up containing radio buttons
that open a local web browser on book-related sites when pressed:



  • The Info pop up displays a simple message line and changes its font every second
    to draw attention to itself; since this can be a bit distracting, the pop up starts out
    iconified (click the Info button to see or hide it).

  • The Links pop up’s radio buttons are much like hyperlinks in a web page, but this
    GUI isn’t a browser: when the Links pop up is pressed, the portable Launch
    Browser script mentioned in Part II is used to find and start a web browser used to
    connect to the relevant site, assuming you have an Internet connection. This in
    turn uses Python’s webbrowser modules today.

  • The windows module we coded earlier in this chapter (Example 10-16) is used to
    give this GUI’s windows a blue “PY” icon, instead of the standard red “Tk.”


The PyDemos GUI also comes with “code” buttons to the right of each demo’s button,
which open the source files that implement the associated example. These files open
in pop-up versions of the PyEdit text editor that we’ll meet in Chapter 11. Fig-
ure 10-18 captures some of these code viewer windows in action, resized slightly for
display here.


For the web-based examples opened by the last two demo buttons in the launcher, this
GUI also attempts to spawn a locally running web server for web-based demos not
shown running here (we’ll meet the server in Chapter 15). For this edition, the web
servers are spawned only when the corresponding web demo button is first selected
(not on PyDemos startup), and the web servers generate a pop-up command prompt
window on Windows to monitor server status.


PyDemos runs on Windows, Macs, and Linux, but that’s largely due to the inherent
portability of both Python and tkinter. For more details, consult the source, which is
shown in part in Example 10-30.


Example 10-30. PP4E\PyDemos.pyw (external)


"""
################################################################################
PyDemos.pyw
Programming Python, 2nd, 3rd, and 4th Editions (PP4E), 2001--2006--2010


Version 2.1 (4E), April '10: updated to run under Python 3.X, and spawn
local web servers for web demos only on first demo button selection.


Version 2.0 (3E), March '06: add source-code file viewer buttons; add new
Demos (PyPhoto, PyMailGUI); spawn locally running web servers for the
browser-based Demos; add window icons; and probably more I've forgotten.


Launch major Python+Tk GUI examples from the book, in a platform-neutral way.
This file also serves as an index to major program examples, though many book


664 | Chapter 10: GUI Coding Techniques

Free download pdf