File: pygadgets-products/unzipped/_PyCalc/Calculator/_PriorCodePP4E/calc0.py
"a simplistic calculator GUI: expressions run all at once with eval/exec" from tkinter import * from PP4E.Gui.Tools.widgets import frame, button, entry class CalcGui(Frame): def __init__(self, parent=None): # an extended frame Frame.__init__(self, parent) # on default top-level self.pack(expand=YES, fill=BOTH) # all parts expandable self.master.title('Python Calculator 0.1') # 6 frames plus entry self.master.iconname("pcalc1") self.names = {} # namespace for variables text = StringVar() entry(self, TOP, text) rows = ["abcd", "0123", "4567", "89()"] for row in rows: frm = frame(self, TOP) for char in row: button(frm, LEFT, char, lambda char=char: text.set(text.get() + char)) frm = frame(self, TOP) for char in "+-*/=": button(frm, LEFT, char, lambda char=char: text.set(text.get()+ ' ' + char + ' ')) frm = frame(self, BOTTOM) button(frm, LEFT, 'eval', lambda: self.eval(text) ) button(frm, LEFT, 'clear', lambda: text.set('') ) def eval(self, text): try: text.set(str(eval(text.get(), self.names, self.names))) # was 'x' except SyntaxError: try: exec(text.get(), self.names, self.names) except: text.set("ERROR") # bad as statement too? else: text.set('') # worked as a statement except: text.set("ERROR") # other eval expression errors if __name__ == '__main__': CalcGui().mainloop()