└── simple cal.py /simple cal.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | def iCalc(source, side): 4 | storeObj = Frame(source, borderwidth=4, bd=4, bg="powder blue") 5 | storeObj.pack(side=side, expand =YES, fill =BOTH) 6 | return storeObj 7 | 8 | def button(source, side, text, command=None): 9 | storeObj = Button(source, text=text, command=command) 10 | storeObj.pack(side=side, expand = YES, fill=BOTH) 11 | return storeObj 12 | 13 | class app(Frame): 14 | def __init__(self): 15 | Frame.__init__(self) 16 | self.option_add('*Font', 'arial 20 bold') 17 | self.pack(expand = YES, fill =BOTH) 18 | self.master.title('Calculator') 19 | 20 | display = StringVar() 21 | Entry(self, relief=RIDGE, textvariable=display, 22 | justify='right' 23 | , bd=30, bg="powder blue").pack(side=TOP, 24 | expand=YES, fill=BOTH) 25 | 26 | for clearButton in (["C"]): 27 | erase = iCalc(self, TOP) 28 | for ichar in clearButton: 29 | button(erase, LEFT, ichar, lambda 30 | storeObj=display, q=ichar: storeObj.set('')) 31 | 32 | for numButton in ("789/", "456*", "123-", "0.+"): 33 | FunctionNum = iCalc(self, TOP) 34 | for iEquals in numButton: 35 | button(FunctionNum, LEFT, iEquals, lambda 36 | storeObj=display, q=iEquals: storeObj 37 | .set(storeObj.get() + q)) 38 | 39 | EqualButton = iCalc(self, TOP) 40 | for iEquals in "=": 41 | if iEquals == '=': 42 | btniEquals = button(EqualButton, LEFT, iEquals) 43 | btniEquals.bind('', lambda e,s=self, 44 | storeObj=display: s.calc(storeObj), '+') 45 | 46 | 47 | else: 48 | btniEquals = button(EqualButton, LEFT, iEquals, 49 | lambda storeObj=display, s=' %s ' % iEquals: storeObj.set 50 | (storeObj.get() + s)) 51 | 52 | def calc(self, display): 53 | try: 54 | display.set(eval(display.get())) 55 | except: 56 | display.set("ERROR") 57 | 58 | 59 | if __name__=='__main__': 60 | app().mainloop() 61 | 62 | --------------------------------------------------------------------------------