GuiStreams: Redirecting Streams to Widgets
The script in Example 11-9 arranges to map input and output sources to pop-up windows in a GUI application, much as we did with strings in the stream redirection topics in Chapter 3. Although this module is really just a first-cut prototype and needs improvement itself (e.g., each input line request pops up a new input dialog), it demonstrates the concepts in general.
Its GuiOutput
and GuiInput
objects define methods that allow
them to masquerade as files in any interface that expects a real file.
As we learned earlier in Chapter
3, this includes standard stream processing tools, such as
print
and raw_input
, and it includes explicit read
and write
calls. The two top-level interfaces in
this module handle common use cases:
The
redirectedGuiFunc
function uses this plug-and-play file compatibility to run a function with its standard input and output streams mapped completely to pop-up windows rather than to the console window (or wherever streams would otherwise be mapped in the system shell).The
redirectedGuiShellCmd
function similarly routes the output of a spawned shell command line to a pop-up window. It can be used to display the output of any program in a GUI—including that printed by a Python program.
The module’s GuiInput
and
GuiOutput
classes can also be used
or customized directly by clients that need more fine-grained control
over the process.
Example 11-9. PP3E\Gui\Tools\guiStreams.py
############################################################################## ...
Get Programming Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.