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

(yzsuai) #1
Speaking of suggested enhancements, in Chapter 9, I showed you a first-
cut way to use images instead of text for buttons in toolbars at the bot-
tom of windows. Adding this option to the GUI maker class as a subclass
which redefines its toolbar construction method would be both a great
way to experiment with the code and a useful utility. If I added every
cool feature imaginable, though, this book could easily become big
enough to be gravitationally significant...

ShellGui: GUIs for Command-Line Tools


Demos are fun, but to better show how things like the GuiMixin class can be of practical
use, we need a more realistic application. Here’s one: suppose you’ve written a set of
command-line system administration scripts, along the lines of those we studied in
Part II. As we saw, such scripts work well from a command line, but require you to
remember all their options each time they are run; if you’re like me, this usually implies
having to pore over the source code after a period of nonuse.


Instead of requiring users of such tools (including yourself) to type cryptic commands
at a shell, why not also provide an easy-to-use tkinter GUI interface for running such
programs? Such a GUI can prompt for command-line inputs, instead of expecting users
to remember them. While we’re at it, why not generalize the whole notion of running
command-line tools from a GUI, to make it easy to support future tools too?


A Generic Shell-Tools Display


Examples 10-5 through 10-11—seven files, spanning two command-line scripts, one
GUI utility module, two GUI dialogs, and a main GUI and its options specification
module—comprise a concrete implementation of these artificially rhetorical musings.
Because I want this to be a general-purpose tool that can run any command-line pro-
gram, its design is factored into modules that become more application-specific as we
go lower in the software hierarchy. At the top, things are about as generic as they can
be, as shown in Example 10-5.


Example 10-5. PP4E\Gui\ShellGui\shellgui.py


#!/usr/local/bin/python
"""
################################################################################
tools launcher; uses guimaker templates, guimixin std quit dialog;
I am just a class library: run mytools script to display the GUI;
################################################################################
"""


from tkinter import # get widgets
from PP4E.Gui.Tools.guimixin import GuiMixin # get quit, not done
from PP4E.Gui.Tools.guimaker import
# menu/toolbar builder


class ShellGui(GuiMixin, GuiMakerWindowMenu): # a frame + maker + mixins


ShellGui: GUIs for Command-Line Tools | 613
Free download pdf