Sometimes, GUIs pop up quite unexpectedly. Perhaps you haven't learned GUI programming yet; or perhaps you're just pining for non-event-driven days past. But for whatever reason, you may have written a program to interact with a user in an interactive console, only to decide later that interaction in a real GUI would be much nicer. What to do?
Probably the real answer to converting a non-GUI program is to
truly convert it—restructure it to initialize widgets on startup, call
mainloop once to start event
processing and display the main window, and move all program logic
into callback functions triggered by user actions. Your original
program's actions become event handlers, and your original main flow
of control becomes a program that builds a main window, calls the
GUI's event loop once, and waits.
This is the traditional way to structure a GUI program, and it
makes for a coherent user experience; windows pop up on request,
instead of showing up at seemingly random times. Until you're ready to
bite the bullet and perform such a structural conversion, though,
there are other possibilities. For example, in the
ShellGui section earlier in this chapter, we
saw how to add windows to file packing scripts to collect inputs; we
also saw how to redirect their outputs to text widgets. This approach
works if the non-GUI operation we're wrapping up in a GUI is a single
operation; for more dynamic user interaction, other techniques might
It's possible, ...