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

(yzsuai) #1

Hence, as shipped:



  • Open uses a passed-in encoding, if any, or else prompts for an encoding name first

  • Save reuses a known encoding if it has one, and otherwise prompts for new file saves

  • Save As always prompts for an encoding name first for the new file

  • Grep allows an encoding to be input in its dialog to apply to the full tree searched


On the other hand, because the platform default will probably work silently without
extra GUI complexity for the vast majority of users anyhow, the textConfig setting can
prevent the pop ups altogether and fall back on an explicit encoding or platform default.
Ultimately, structuring encoding selection well requires the sort of broad user experi-
ence and feedback which is outside this book’s scope, not the guesses of a single de-
veloper. As always, feel free to tailor as you like.


See the test subdirectory in the examples for a few Unicode text files to experiment
with opening and saving, in conjunction with textConfig changes. As suggested when
we saw Figures 11-5 and 11-6, this directory contains files that use International char-
acter sets, saved in different encodings. For instance, file email-part--koi8-r there is
formatted per the Russian encoding koi8-r, and email-part--koi8-r--utf8 is the same file
saved in UTF-8 encoding format; the latter works well in Notepad on Windows, but
the former will only display properly when giving an explicit encoding name to PyEdit.


Better yet, make a few Unicode files yourself, by changing textConfig to hardcode
encodings or always ask for encodings—thanks largely to Python 3.X’s Unicode sup-
port, PyEdit allows you to save and load in whatever encoding you wish.


More on Quit checks: The event revisited


Before we get to the code, one of version 2.1’s changes merits a few additional words,
because it illustrates the fundamentals of tkinter window closure in a realistic context.
We learned in Chapter 8 that tkinter also has a event for the bind method
which is run when windows and widgets are destroyed. Although we could bind this
event on PyEdit windows or their text widgets to catch destroys on program exit, this
won’t quite help with the use case here. Scripts cannot generally do anything GUI-
related in this event’s callback, because the GUI is being torn down. In particular, both
testing a text widget for modifications and fetching its content in a handler
can fail with an exception. Popping up a save verification dialog at this point may act
oddly, too: it only shows up after some of the window’s widgets may have already been
erased (including the text widget whose contents the user may wish to inspect and
save!), and it might sometimes refuse to go away altogether.


As also mentioned in Chapter 8, running a quit method call does not trigger any


events, but does trigger a fatal Python error message on exit. To use destroy
events at all, PyEdit would have to be redesigned to close windows on Quit requests
with the destroy method only, and rely on the Tk root window destruction protocol for
exits; immediate shutdowns would be unsupported, or require tools such as

692 | Chapter 11: Complete GUI Programs

Free download pdf