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

(yzsuai) #1

  • More usefully, we could pass in the fieldnames tuple as an input parameter to the
    functions here to allow them to be used for other record types in the future. Code
    at the bottom of the file would similarly become a function with a passed-in shelve
    filename, and we would also need to pass in a new record construction call to the
    update function because Person could not be hardcoded. Such generalization is
    beyond the scope of this preview, but it makes for a nice exercise if you are so
    inclined. Later, I’ll also point you to a suggested reading example in the book
    examples package, PyForm, which takes a different approach to generalized form
    construction.

  • To make this GUI more user friendly, it might also be nice to add an index window
    that displays all the keys in the database in order to make browsing easier. Some
    sort of verification before updates might be useful as well, and Delete and Clear
    buttons would be simple to code. Furthermore, assuming that inputs are Python
    code may be more bother than it is worth; a simpler input scheme might be easier
    and safer. (I won’t officially say these are suggested exercises too, but it sounds like
    they could be.)

  • We could also support window resizing (as we’ll learn, widgets can grow and shrink
    with the window) and provide an interface for calling methods available on stored
    instances’ classes too (as is, the pay field can be updated, but there is no way to
    invoke the giveRaise method).

  • If we plan to distribute this GUI widely, we might package it up as a standalone
    executable program—a frozen binary in Python terminology—using third-party
    tools such as Py2Exe, PyInstaller, and others (search the Web for pointers). Such
    a program can be run directly without installing Python on the receiving end, be-
    cause the Python bytecode interpreter is included in the executable itself.


I’ll leave all such extensions as points to ponder, and revisit some of them later in this
book.


Before we move on, two notes. First, I should mention that even more graphical pack-
ages are available to Python programmers. For instance, if you need to do graphics
beyond basic windows, the tkinter Canvas widget supports freeform graphics. Third-
party extensions such as Blender, OpenGL, VPython, PIL, VTK, Maya, and PyGame
provide even more advanced graphics, visualization, and animation tools for use with
Python scripts. Moreover, the PMW, Tix, and ttk widget kits mentioned earlier extend
tkinter itself. See Python’s library manual for Tix and ttk, and try the PyPI site or a web
search for third-party graphics extensions.


And in deference to fans of other GUI toolkits such as wxPython and PyQt, I should
also note that there are other GUI options to choose from and that choice is sometimes
very subjective. tkinter is shown here because it is mature, robust, fully open source,
well documented, well supported, lightweight, and a standard part of Python. By most
accounts, it remains the standard for building portable GUIs in Python.


50 | Chapter 1: A Sneak Preview

Free download pdf