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

(yzsuai) #1

Notice how we’re using repr again to display field values fetched from the shelve and
eval to convert field values to Python objects before they are stored in the shelve. As
mentioned previously, this is potentially dangerous if someone sneaks some malicious
code into our shelve, but we’ll finesse such concerns for now.


Keep in mind, though, that this scheme means that strings must be quoted in input
fields other than the key—they are assumed to be Python code. In fact, you could type
an arbitrary Python expression in an input field to specify a value for an update. Typing
"Tom"*3 in the name field, for instance, would set the name to TomTomTom after an update
(for better or worse!); fetch to see the result.


Even though we now have a GUI for browsing and changing records, we can still check
our work by interactively opening and inspecting the shelve file or by running scripts
such as the dump utility in Example 1-19. Remember, despite the fact that we’re now
viewing records in a GUI’s windows, the database is a Python shelve file containing
native Python class instance objects, so any Python code can access it. Here is the dump
script at work after adding and changing a few persistent objects in the GUI:


...\PP4E\Preview> python dump_db_classes.py
sue =>
Sue Jones 50000.0
bill =>
bill 9999
nobody =>
John Doh None
tomtom =>
Tom Tom 40000
tom =>
Tom Doe 90000
bob =>
Bob Smith 30000
peg =>
1 4
Smith
Doe

Future directions


Although this GUI does the job, there is plenty of room for improvement:



  • As coded, this GUI is a simple set of functions that share the global list of input
    fields (entries) and a global shelve (db). We might instead pass db in to
    makeWidgets, and pass along both these two objects as function arguments to the
    callback handlers using the lambda trick of the prior section. Though not crucial in
    a script this small, as a rule of thumb, making your external dependencies explicit
    like this makes your code both easier to understand and reusable in other contexts.

  • We could also structure this GUI as a class to support attachment and customiza-
    tion (globals would become instance attributes), though it’s unlikely that we’ll need
    to reuse such a specific GUI.


Step 5: Adding a GUI | 49
Free download pdf