-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
121 lines (89 loc) · 5.53 KB
/
calculator.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from tkinter import *
class calculate():
def __init__(self):
self.root = Tk()
self.root.title("Calculator")
self.root.geometry("290x260")
self.root.maxsize(290, 260)
self.root.minsize(290, 260)
self.root.config(bg="grey")
self.resultwindow = Entry(self.root,borderwidth=5, relief=SUNKEN)
self.resultwindow.grid(row=0,column=0,columnspan=6,pady=5)
self.resultwindow.config(font=("Arial", 18))
self.resultwindow.focus_set() # Sets focus on the input text area
# Buttons
self.button1 = Button(self.root, text="1", width=3, command=lambda:self.ins('1'),relief=RAISED,bg='light green')
self.button1.grid(row=1,column=0, padx=3, pady=3)
self.button1.config(font=("Arial", 18))
self.button2 = Button(self.root, text="2", width=3, command=lambda:self.ins('2'),relief=RAISED,bg='light green')
self.button2.grid(row=1, column=1, padx=3, pady=3)
self.button2.config(font=("Arial", 18))
self.button3 = Button(self.root, text="3", width=3, command=lambda:self.ins('3'),relief=RAISED,bg='light green')
self.button3.grid(row=1, column=2, padx=3, pady=3)
self.button3.config(font=("Arial", 18))
self.button4 = Button(self.root, text="4", width=3, command=lambda:self.ins('4'),relief=RAISED,bg='light green')
self.button4.grid(row=2, column=0, padx=3, pady=3)
self.button4.config(font=("Arial", 18))
self.button5 = Button(self.root, text="5", width=3, command=lambda:self.ins('5'),relief=RAISED,bg='light green')
self.button5.grid(row=2, column=1, padx=3, pady=3)
self.button5.config(font=("Arial", 18))
self.button6 = Button(self.root, text="6", width=3, command=lambda:self.ins('6'),relief=RAISED,bg='light green')
self.button6.grid(row=2, column=2, padx=3, pady=3)
self.button6.config(font=("Arial", 18))
self.button7 = Button(self.root, text="7", width=3, command=lambda:self.ins('7'),relief=RAISED,bg='light green')
self.button7.grid(row=3, column=0, padx=3, pady=3)
self.button7.config(font=("Arial", 18))
self.button8 = Button(self.root, text="8", width=3, command=lambda:self.ins('8'),relief=RAISED,bg='light green')
self.button8.grid(row=3, column=1, padx=3, pady=3)
self.button8.config(font=("Arial", 18))
self.button9 = Button(self.root, text="9", width=3, command=lambda:self.ins('9'),relief=RAISED,bg='light green')
self.button9.grid(row=3, column=2, padx=3, pady=3)
self.button9.config(font=("Arial", 18))
self.button0 = Button(self.root, text="0", width=3, command=lambda: self.ins('0'),relief=RAISED,bg='light green')
self.button0.grid(row=4, column=0, padx=3, pady=3)
self.button0.config(font=("Arial", 18))
self.button_open = Button(self.root, text="(", width=3, command=lambda: self.ins('('),relief=RAISED)
self.button_open.grid(row=4, column=1, padx=3, pady=3)
self.button_open.config(font=("Arial", 18))
self.button_close = Button(self.root, text=")", width=3, command=lambda: self.ins(')'),relief=RAISED)
self.button_close.grid(row=4, column=2, padx=3, pady=3)
self.button_close.config(font=("Arial", 18))
# Operations Buttons
self.buttonplus = Button(self.root, text="+", width=3, command=lambda:self.ins('+'),relief=RAISED)
self.buttonplus.grid(row=1, column=3, padx=3, pady=3)
self.buttonplus.config(font=("Arial", 18))
self.buttonminus = Button(self.root, text="-", width=3, command=lambda:self.ins('-'),relief=RAISED)
self.buttonminus.grid(row=1, column=4, padx=3, pady=3)
self.buttonminus.config(font=("Arial", 18))
self.buttondivide = Button(self.root, text="/", width=3, command=lambda:self.ins('/'),relief=RAISED)
self.buttondivide.grid(row=2, column=3, padx=3, pady=3)
self.buttondivide.config(font=("Arial", 18))
self.buttonmultiply = Button(self.root, text="*", width=3, command=lambda:self.ins('*'),relief=RAISED)
self.buttonmultiply.grid(row=2, column=4, padx=3, pady=3)
self.buttonmultiply.config(font=("Arial", 18))
self.buttoncancel = Button(self.root, text="C", width=3, command=lambda: self.cancel(),relief=RAISED,bg='#EF5350',fg='white')
self.buttoncancel.grid(row=3, column=4, padx=3, pady=3)
self.buttoncancel.config(font=("Arial", 18))
self.buttondeleteall = Button(self.root, text="Del", width=3, command=lambda: self.delete_all(),relief=RAISED)
self.buttondeleteall.grid(row=3, column=3, padx=3, pady=3)
self.buttondeleteall.config(font=("Arial", 18))
self.buttonresult = Button(self.root, text="=", width=7, command=lambda:self.calculate(),relief=RAISED,bg='#FFEE58')
self.buttonresult.grid(row=4, column=3, padx=3, pady=3, columnspan=2)
self.buttonresult.config(font=("Arial", 18))
self.root.mainloop()
def ins(self,val):
self.resultwindow.insert(END, val)
def cancel(self):
self.resultwindow.delete(0, 'end')
def delete_all(self):
x = self.resultwindow.get()
self.resultwindow.delete(0, 'end')
y = x[:-1]
self.resultwindow.insert(0, y)
def calculate(self):
x = self.resultwindow.get()
answer = eval(x)
self.resultwindow.delete(0, 'end')
self.resultwindow.insert(0, answer)
if __name__ == "__main__":
calculate()