if name == 'main':
import sys
if len(sys.argv) == 1: # % calculator_test.py
root = Tk() # run 3 calcs in same process
CalcGui(Toplevel()) # each in a new toplevel window
calcContainer(Toplevel())
calcSubclass(Toplevel())
Button(root, text='quit', command=root.quit).pack()
root.mainloop()
if len(sys.argv) == 2: # % calculator_testl.py -
CalcGui().mainloop() # as a standalone window (default root)
elif len(sys.argv) == 3: # % calculator_test.py - -
calcContainer().mainloop() # as an embedded component
elif len(sys.argv) == 4: # % calculator_test.py - - -
calcSubclass().mainloop() # as a customized superclass
Figure 19-7 shows the result of running this script with no command-line arguments.
We get instances of the original calculator class, plus the container and subclass classes
defined in this script, all attached to new top-level windows.
Figure 19-7. The calculator_test script: attaching and extending
These two windows on the right reuse the core PyCalc code running in the window on
the left. All of these windows run in the same process (e.g., quitting one quits them all),
but they all function as independent windows. Note that when running three calcula-
tors in the same process like this, each has its own distinct expression evaluation name-
space because it’s a class instance attribute, not a global module-level variable. Because
of that, variables set in one calculator are set in that calculator only, and they don’t
overwrite settings made in other windows. Similarly, each calculator has its own eval-
uation stack manager object, such that calculations in one window don’t appear in or
impact other windows at all.
1476 | Chapter 19: Text and Language