-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculater.py
57 lines (45 loc) · 2.05 KB
/
calculater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from tkinter import *
def iCalc(source, side):
storeobj = Frame(source, borderwidth=5, bd=3, bg="light blue")
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj
def button(source, side, text, command=None):
storeobj = Button(source, text=text, command=command)
storeobj.pack(side=side, expand=YES, fill=BOTH)
return storeobj
class app(Frame):
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'arial 25 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('My calculator')
display = StringVar()
Entry(self, relief=RIDGE,
textvariable=display, justify='right', bd=30, bg="pink").pack(side=TOP, expand=YES,
fill=BOTH)
for clearBut in (["CE"], ["C"]):
erase = iCalc(self, TOP)
for ichar in clearBut:
button(erase, LEFT, ichar,
lambda storeObj=display, q=ichar: storeObj.set(''))
for NumBut in ("#@%!^", "789*/", "456+-", "123()", "0.{}√"):
FunctionNum = iCalc(self, TOP)
for iEquals in NumBut:
button(FunctionNum, LEFT, iEquals,
lambda storeObj=display, q=iEquals: storeObj.set(storeObj.get() + q))
EqualsButton = iCalc(self, TOP)
for iEquals in "=":
if iEquals == '=':
btniEquals = button(EqualsButton, LEFT, iEquals)
btniEquals.bind('<ButtonRelease-1>',
lambda e, s=self, storeObj=display: s.calc(storeObj), '+')
else:
btniEquals = button(EqualsButton, LEFT, iEquals,
lambda storeObj=display, s=' %s ' % iEquals: storeObj.set(storeObj.get() + s))
def calc(self, display):
try:
display.set(eval(display.get()))
except:
display.set("ERROR")
if __name__ == '__main__':
app().mainloop()