mytools.py in Example 10-6 to register code like the function wrapper here as its call-
back handlers.
In fact, you can use this technique to route the output of any function call or command
line to a pop-up window; as usual, the notion of compatible object interfaces is at the
heart of much of Python code’s flexibility.
Reloading Callback Handlers Dynamically
Our next GUI programming technique is all about changing a GUI while it is running—
the ultimate in customization. The Python imp.reload function lets you dynamically
change and reload a program’s modules without stopping the program. For instance,
you can bring up a text editor window to change the source code of selected parts of a
system while it is running and see those changes show up immediately after reloading
the changed module.
This is a powerful feature, especially for developing programs that take a long time to
restart. Programs that connect to databases or network servers, initialize large objects,
implement long-running services, or travel through a long series of steps to retrigger a
callback are prime candidates for reload. It can shave substantial time from the devel-
opment cycle and make systems more flexible.
The catch for GUIs, though, is that because callback handlers are registered as object
references rather than module and object names, reloads of callback handler functions
are ineffective after the callback has been registered. The Python imp.reload operation
works by changing a module object’s contents in place. Because tkinter stores a pointer
to the registered handler object directly, though, it is oblivious to any reloads of the
module that the handler came from. That is, tkinter will still reference a module’s old
objects even after the module is reloaded and changed.
This is a subtle thing, but you really only need to remember that you must do something
special to reload callback handler functions dynamically. Not only do you need to
Figure 10-9. Routing script outputs to GUI pop ups
628 | Chapter 10: GUI Coding Techniques