-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculator GUI Simple Program.py
More file actions
220 lines (170 loc) · 6.06 KB
/
Calculator GUI Simple Program.py
File metadata and controls
220 lines (170 loc) · 6.06 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from tkinter import *
import re
root=Tk()
txt=''
canva1=Canvas(root,height=80,width=300,background='white')
canva1.pack()
canva2=Canvas(root,height=400,width=400 , background='white')
canva2.pack()
def remove_leading_zeros(expression):
pattern = r'\b0+(\d+)'
repl = r'\1'
result = re.sub(pattern, repl, expression)
return result
def one(x=1):
global txt
txt += '1'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def two(x=1):
global txt
txt += '2'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def three(x=1):
global txt
txt += '3'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def four(x=1):
global txt
txt += '4'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def five(x=1):
global txt
txt += '5'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def six(x=1):
global txt
txt += '6'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def seven(x=1):
global txt
txt += '7'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def eight(x=1):
global txt
txt += '8'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def nine(x=1):
global txt
txt += '9'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def zero(x=1):
global txt
txt += '0'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def mins(x=1):
global txt
if len(txt)!=0:
if txt[-1] in '+-*/':
txt=txt[:-1:]
txt += '-'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def div(x=1):
global txt
if len(txt)!=0:
if txt[-1] in '+-*/':
txt=txt[:-1:]
txt += '/'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def mult(x=1):
global txt
if len(txt)!=0:
if txt[-1] in '+-*/':
txt=txt[:-1:]
txt += '*'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def plus(x=1):
global txt
if len(txt)!=0:
if txt[-1] in '+-*/':
txt=txt[:-1:]
txt += '+'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def equal(x=1):
global txt
try:
txt=remove_leading_zeros(txt)
txt=str(eval(txt))
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
except:
canva1.delete('all')
canva1.create_text(150, 40, text='INVALID !!!', fill="black", font=('Helvetica 15'))
def c(x=1):
global txt
txt=txt[:-1]
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
def point(x=1):
global txt
if txt[-1].isnumeric():
txt+='.'
canva1.delete('all')
canva1.create_text(150, 40, text=txt, fill="black", font=('Helvetica 15'))
b_one = Button(canva2, text='1',foreground='white', borderwidth=0,background='#505050',width=10, height=2, command=one)
b_one.grid(row=1, column=1)
b_two = Button(canva2, text='2',foreground='white', borderwidth=0,background='#505050',width=10, height=2 , command=two)
b_two.grid(row=1, column=2)
b_three = Button(canva2, text='3',foreground='white', borderwidth=0,background='#505050',width=10, height=2 ,command=three)
b_three.grid(row=1, column=3)
b_four = Button(canva2, text='4',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=four)
b_four.grid(row=2, column=1)
b_five = Button(canva2, text='5',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=five)
b_five.grid(row=2, column=2)
b_six = Button(canva2, text='6',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=six)
b_six.grid(row=2, column=3)
b_seven = Button(canva2, text='7',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=seven)
b_seven.grid(row=3, column=1)
b_eight = Button(canva2, text='8',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=eight)
b_eight.grid(row=3, column=2)
b_nine = Button(canva2, text='9', foreground='white', borderwidth=0,background='#505050',width=10, height=2 ,command=nine)
b_nine.grid(row=3, column=3)
b_zero = Button(canva2, text='0',foreground='white', borderwidth=0,background='#505050', width=10, height=2 ,command=zero)
b_zero.grid(row=4, column=2)
b_plus = Button(canva2, text='+',width=10,borderwidth=1, height=2 ,command=plus)
b_plus.grid(row=1, column=4)
b_mins = Button(canva2, text='-', width=10,borderwidth=1, height=2 ,command=mins)
b_mins.grid(row=2, column=4)
b_mult = Button(canva2, text='*', width=10,borderwidth=1, height=2 ,command=mult)
b_mult.grid(row=3, column=4)
b_div = Button(canva2, text="/",width=10, borderwidth=1,height=2 ,command=div)
b_div.grid(row=4, column=4)
b_equal = Button(canva2, text="=",width=10, borderwidth=1,background='#FF9500',height=2 ,command=equal)
b_equal.grid(row=5, column=4)
b_c = Button(canva2, text="C",width=10, borderwidth=1,height=2 ,command=c)
b_c.grid(row=4, column=3)
b_point = Button(canva2, text=".",width=10, borderwidth=1,height=2 ,command=point)
b_point.grid(row=4, column=1)
root.bind('1', one)
root.bind('2', two)
root.bind('3', three)
root.bind('4', four)
root.bind('5', five)
root.bind('6', six)
root.bind('7', seven)
root.bind('8', eight)
root.bind('9', nine)
root.bind('0',zero)
root.bind('<Return>', equal)
root.bind('=', equal)
root.bind('-', mins)
root.bind('+', plus)
root.bind('*', mult)
root.bind('/', div)
root.bind('<BackSpace>', c)
root.bind('c', c)
root.bind('.', point)
mainloop()