-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.py
181 lines (117 loc) · 4.41 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
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
#importing packages
from tkinter import *
import re
#initiate windows
root = Tk()
root.geometry("379x278")
root.title("Basic Calculator")
#global declaration
#flag just keep track that multiple sign not pressed at a time and eqal is not pressed after a sign
flag=False
#after the result of expression clear the display
end=False
#expreesin is the variable that stores the buttones those are clicked,initially empty
global expression
expression=''
#button for numeric input
button_1=Button(root,text="1",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(1)).grid(row=3,column=0)
button_2=Button(root,text="2",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(2)).grid(row=3,column=1)
button_3=Button(root,text="3",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(3)).grid(row=3,column=2)
button_4=Button(root,text="4",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(4)).grid(row=2,column=0)
button_5=Button(root,text="5",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(5)).grid(row=2,column=1)
button_6=Button(root,text="6",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(6)).grid(row=2,column=2)
button_7=Button(root,text="7",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(7)).grid(row=1,column=0)
button_8=Button(root,text="8",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(8)).grid(row=1,column=1)
button_9=Button(root,text="9",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(9)).grid(row=1,column=2)
button_0=Button(root,text="0",padx=40,pady=20, borderwidth=1,command=lambda :fun_num(0)).grid(row=4,column=0)
#button for calculatopn button
button_mul=Button(root,text="*",padx=40,pady=20, borderwidth=1,command=lambda:fun_cal('*')).grid(row=3,column=3)
button_sub=Button(root,text="-",padx=42,pady=20, borderwidth=1,command=lambda:fun_cal('-')).grid(row=2,column=3)
button_add=Button(root,text="+",padx=40,pady=20, borderwidth=1,command=lambda:fun_cal('+')).grid(row=1,column=3)
button_divide=Button(root,text="/",padx=40,pady=20, borderwidth=1,command=lambda:fun_cal('/')).grid(row=4,column=1)
button_clear=Button(root,text="clear",padx=30,pady=20, borderwidth=1,command=lambda:fun_clrscrn()).grid(row=4,column=2)
button_equal=Button(root,text="=",padx=41,pady=20, borderwidth=1,command=lambda:fun_equal()).grid(row=4,column=3)
#entry for display result
#resultval=IntVar()
result=Entry(root,borderwidth=5,width=60 )
result.grid(row=0,column=0,columnspan=30)
#function for get the numeric button's value
def fun_num(num):
global expression
global flag
global end
if end:
result.delete(0, END)
pre_num=result.get()
result.delete(0,END)
result.insert(0,str(pre_num)+str((num)))
expression=(result.get())
flag=True
end=False
#function for get the sign button's value
def fun_cal(num):
global flag
global end
if end:
result.delete(0, END)
if flag:
global expression
pre_num=result.get()
result.delete(0,END)
result.insert(0,str(pre_num)+str(num))
expression=result.get()
flag= False
end=False
#function for validating the text field.take only numeric value and discard other keys
'''
def validateinput(input):
global end
if input.isdigit():
if end:
result.delete(0, END)
end=False
return True
elif input== '+':
return True
else:
return False
checkinput=root.register(validateinput)
result.config(validate="key",validatecommand=(checkinput,'%P'))
'''
#function for clear the screen
def fun_clrscrn():
result.delete(0,END)
#function for removing leading zeroes of a number as it indicates octal
def remove_leading_zero(txt):
new = ""
count = 1
x = re.split(r"\D+", txt)
y = re.split("\d+", txt)
sign_len = len(y)
for i in range(0, len(x)):
x[i] = str(int(x[i]))
for i in x:
new = new + i
if sign_len != 1:
new = new + y[count]
sign_len-=1
count += 1
return new
#function for evaluate the expression
def fun_equal():
global expression
global flag
global end
global s
expression = remove_leading_zero(expression)
if flag:
try:
result.delete(0, END)
result.insert(0,eval(expression))
except Exception as e:
s=str(e)
result.insert(0,s)
finally:
end=True
flag = False
root.mainloop()