-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.py
More file actions
53 lines (39 loc) · 1.52 KB
/
cal.py
File metadata and controls
53 lines (39 loc) · 1.52 KB
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
#author-Harfho
#simple calculator
from tkinter import *
def frame(root, side):
w = Frame(root)
w.pack(side=side, expand=YES, fill=BOTH)
return w
def button(root, side, text, command=None):
w = Button(root, text=text, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w
class Calculator(Frame):
def __init__(self):
Frame.__init__(self)
self.pack(expand=YES, fill=BOTH)
self.master.title('Simple Calculator By Harfho')
self.master.iconname("calc1")
display = StringVar()
Entry(self, relief=SUNKEN,textvariable=display).pack(side=TOP, expand=YES,fill=BOTH)
for key in ("123", "456", "789", "-0."):
keyF = frame(self, TOP)
for char in key:
button(keyF, LEFT, char,lambda w=display, s=' %s '%char: w.set(w.get()+s))
opsF = frame(self, TOP)
for char in "+-*/=":
if char == '=':
btn = button(opsF, LEFT, char)
btn.bind('<ButtonRelease-1>',lambda e, s=self, w=display: s.calc(w), '+')
else:
btn = button(opsF, LEFT, char,lambda w=display, c=char: w.set(w.get()+' '+c+' '))
clearF = frame(self, BOTTOM)
button(clearF, LEFT, 'Clr', lambda w=display: w.set(''))
def calc(self, display):
try:
display.set(eval(display.get()))
except ValueError:
display.set("ERROR")
#if __name__ == '__main__':
Calculator().mainloop()