exec(code, self.names, self.names) # try stmt: None
self.hist.append(code)
result = None
return result
def getHist(self):
return '\n'.join(self.hist)
def getCalcArgs():
from sys import argv # get cmdline args in a dict
config = {} # ex: -bg black -fg red
for arg in argv[1:]: # font not yet supported
if arg in ['-bg', '-fg']: # -bg red' -> {'bg':'red'}
try:
config[arg[1:]] = argv[argv.index(arg) + 1]
except:
pass
return config
if name == 'main':
CalcGui(**getCalcArgs()).mainloop() # in default toplevel window
Using PyCalc as a component
PyCalc serves a standalone program on my desktop, but it’s also useful in the context
of other GUIs. Like most of the GUI classes in this book, PyCalc can be customized
with subclass extensions or embedded in a larger GUI with attachments. The module
in Example 19-21 demonstrates one way to reuse PyCalc’s CalcGui class by extending
and embedding, similar to what was done for the simple calculator earlier.
Example 19-21. PP4E\Lang\Calculator\calculator_test.py
"""
test calculator: use as an extended and embedded GUI component
"""
from tkinter import *
from calculator import CalcGui
def calcContainer(parent=None):
frm = Frame(parent)
frm.pack(expand=YES, fill=BOTH)
Label(frm, text='Calc Container').pack(side=TOP)
CalcGui(frm)
Label(frm, text='Calc Container').pack(side=BOTTOM)
return frm
class calcSubclass(CalcGui):
def makeWidgets(self, fg, bg, font):
Label(self, text='Calc Subclass').pack(side=TOP)
Label(self, text='Calc Subclass').pack(side=BOTTOM)
CalcGui.makeWidgets(self, fg, bg, font)
#Label(self, text='Calc Subclass').pack(side=BOTTOM)
PyCalc: A Calculator Program/Object | 1475