- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculator.py
More file actions
Latest commit
202 lines (159 loc) · 5.81 KB
/
Copy pathCalculator.py
File metadata and controls
202 lines (159 loc) · 5.81 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#author: Vijesh.V @FreakoutGames
#building a calculator using Tkinter
importtkinterastk
#intialize
root=tk.Tk()
root.title("Simple Calculator")
#input field
enteredNumbers=tk.Entry(root, width=35, borderwidth=2)
enteredNumbers.grid(row=0, column=0, columnspan=3, ipadx=19, ipady=3)
enter=tk.Entry(root, width=35, borderwidth=10)
enter.grid(row=1, column=0, columnspan=3, ipadx=10, ipady=10)
current=""
defbutton_click(number):
globalcurrent
current=current+str(number)
enter.delete(0, tk.END)
enter.insert(0, current)
defclearGlobalVariables():
globalcurrent
globaloperandCount
globalresult
globalequation
current=""
operandCount=0
result=0
operandList.clear()
operatorsList.clear()
equation.clear()
print(equation)
defcleardisplayNumbers():
enteredNumbers.delete(0, tk.END)
defbutton_clear():
enter.delete(0,tk.END)
enteredNumbers.delete(0, tk.END)
clearGlobalVariables()
defbutton_add():
operatorHandler("+")
defoperatorHandler(operator):
globalcurrent
globaloperatorsList
globalequation
ifcurrent==""andlen(operatorsList) >0andoperatorsList[len(operatorsList)-1] ==operator:
return
ifcurrent=="":
operatorsList[len(operatorsList)-1] =operator
equation[len(equation)-1] =operator
displayNumbers()
return
else:
stackEquation(current, operator)
operatorsList= []
operandList= []
defstackEquation(element, operator):
globaloperandList
globaloperatorsList
globalcurrent
globalequation
current=""
operatorsList.append(operator)
operandList.append(element)
equation.append(operandList[len(operandList)-1])
equation.append(operatorsList[len(operatorsList)-1])
displayNumbers()
doCalculation(operandList, operatorsList)
result=0
operandCount=0
defdoCalculation(operands, operators):
#global operandAndoperator
globalresult
globaloperandCount
sum=0
print(result)
iflen(operands) <2:
return
else:
ifoperandCount==0:
result=int(operands[0])
ifoperators[operandCount] =="+":
sum=int(result) +int(operands[operandCount+1])
result=sum
#print(result)
operandCount=operandCount+1
enter.delete(0, tk.END)
enter.insert(0, result)
elifoperators[operandCount] =="-":
sum=int(result) -int(operands[operandCount+1])
result=sum
#print(result)
operandCount=operandCount+1
enter.delete(0, tk.END)
enter.insert(0, result)
elifoperators[operandCount] =="/":
sum=int(result) /int(operands[operandCount+1])
result=sum
#print(result)
operandCount=operandCount+1
enter.delete(0, tk.END)
enter.insert(0, result)
elifoperators[operandCount] =="*":
sum=int(result) *int(operands[operandCount+1])
result=sum
#print(result)
operandCount=operandCount+1
enter.delete(0, tk.END)
enter.insert(0, result)
print(operands)
defbutton_equalfunc():
globalresult
second_number=enter.get()
enter.delete(0, tk.END)
stackEquation(second_number, operatorsList[len(operatorsList)-1])
#clearGlobalVariables()
cleardisplayNumbers()
return
equation= []
defdisplayNumbers():
globalequation
enteredNumbers.delete(0, tk.END)
enteredNumbers.insert(0,equation)
defbutton_subtract():
operatorHandler("-")
defbutton_division():
operatorHandler("/")
defbutton_multiply():
operatorHandler("*")
button_1=tk.Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2=tk.Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3=tk.Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4=tk.Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5=tk.Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6=tk.Button(root, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7=tk.Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8=tk.Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9=tk.Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0=tk.Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add=tk.Button(root, text="+", padx=39, pady=20, command=button_add)
button_sub=tk.Button(root, text="-", padx=39, pady=20, command=button_subtract)
button_div=tk.Button(root, text="/", padx=39, pady=20, command=button_division)
button_mul=tk.Button(root, text="*", padx=39, pady=20, command=button_multiply)
button_clear=tk.Button(root, text="clear", padx=78, pady=20, command=button_clear)
button_equal=tk.Button(root, text="=", padx=86, pady=20, command=button_equalfunc)
#button_substract= tk.Button(root, text="-", padx= 39, pady=20, command= button_add)
button_1.grid(row=4, column=0)
button_2.grid(row=4, column=1)
button_3.grid(row=4, column=2)
button_4.grid(row=3, column=0)
button_5.grid(row=3, column=1)
button_6.grid(row=3, column=2)
button_7.grid(row=2, column=0)
button_8.grid(row=2, column=1)
button_9.grid(row=2, column=2)
button_0.grid(row=5, column=0)
button_add.grid(row=6, column=0)
button_sub.grid(row=7, column=0)
button_div.grid(row=7, column=1)
button_mul.grid(row=7, column=2)
button_clear.grid(row=5, column=1, columnspan=3)
button_equal.grid(row=6, column=1, columnspan=3)
root.mainloop()