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

(yzsuai) #1

simpleShell.py in the PyEdit’s directory if you wish to experiment with this), though
you need a bit more to handle multiple-line statements and expression result displays:


# read and run Python statement strings: like PyEdit's run code menu option
namespace = {}
while True:
try:
line = input('>>> ') # single-line statements only
except EOFError:
break
else:
exec(line, namespace) # or eval() and print result

Depending on the user’s preference, PyEdit either does something similar to this to run
code fetched from the text widget or uses the launchmodes module we wrote at the end
of Chapter 5 to run the code’s file as an independent program. There are a variety of
options in both schemes that you can customize as you like (this is Python, after all).
See the onRunCode method for details or simply edit and run some Python code in PyEdit
on your own to experiment. When edited code is run in nonfile mode, you can view
its printed output in PyEdit’s console window. As we footnoted about eval and exec
in Chapter 9, also make sure you trust the source of code you run this way; it has all
permissions that the Python process does.


Multiple windows


PyEdit not only pops up multiple special-purpose windows, it also allows multiple edit
windows to be open concurrently, in either the same process or as independent pro-
grams. For illustration, Figure 11-3 shows three independently started instances of
PyEdit, resized and running with a variety of color schemes and fonts. Since these are
separate programs, closing any of these does not close the others. This figure also cap-
tures PyEdit torn-off menus at the bottom and the PyEdit help pop up on the right. The
edit windows’ backgrounds are shades of green, red, and blue; use the Tools menu’s
Pick options to set colors as you like.


Since these three PyEdit sessions are editing Python source-coded text, you can run
their contents with the Run Code option in the Tools pull-down menu. Code run from
files is spawned independently; the standard streams of code run not from a file (i.e.,
fetched from the text widget itself) are mapped to the PyEdit session’s console window.
This isn’t an IDE by any means; it’s just something I added because I found it to be
useful. It’s nice to run code you’re editing without fishing through directories.


To run multiple edit windows in the same process, use the Tools menu’s Clone option
to open a new empty window without erasing the content of another. Figure 11-4 shows
the single-process scene with a window and its clone, along with pop-ups related to
the Search menu’s Grep option, described in the next section—a tool that walks di-
rectory trees in parallel threads, collecting files of matching names that contain a search
string, and opening them on request. In Figure 11-4, Grep has produced an input dialog,


678 | Chapter 11: Complete GUI Programs

Free download pdf