-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
194 lines (149 loc) · 5 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
182
183
184
185
186
187
188
189
190
191
192
193
194
from tkinter import *
import math
from pygame import mixer
import speech_recognition
mixer.init()
def click(value):
ex=entryField.get()
ans=''
try:
if value=='C':
ex=ex[0:len(ex)-1]
entryField.delete(0,END)
entryField.insert(0,ex)
return
elif value=='CE':
entryField.delete(0,END)
elif (value == '√'):
ans = math.sqrt(eval(ex))
elif (value == 'π'):
ans = math.pi
elif (value == 'cos'):
ans = math.cos(math.radians(eval(ex)))
elif (value == 'tan'):
ans = math.tan(math.radians(eval(ex)))
elif (value == 'sin'):
ans = math.sin(math.radians(eval(ex)))
elif (value == '1/x'):
ans = 1/eval(ex)
elif (value == 'cosh'):
ans = math.cosh(eval(ex))
elif (value == 'tanh'):
ans = math.tanh(eval(ex))
elif (value == 'sinh'):
ans = math.sinh(eval(ex))
elif(value == chr(8731)):
ans = eval(ex)**(1/3)
elif(value == 'x\u02b8'):
ans = entryField.insert(END, '**')
return
elif(value == 'x\u00b3'):
ans = eval(ex)**3
elif(value == 'x\u00b2'):
ans = eval(ex)**2
elif(value=='log10'):
ans=math.log10(eval(ex))
elif(value=='x!'):
ans=math.factorial(ex)
elif(value == 'ln'):
ans = math.log2(eval(ex))
elif(value == 'deg'):
ans = math.degrees(eval(ex))
elif(value == 'rad'):
ans = math.radians(eval(ex))
elif(value==chr(247)):
entryField.insert(END, "/")
return
elif value == '=':
ans= eval(ex)
else:
entryField.insert (END,value)
return
entryField.delete(0, END)
entryField.insert(0, ans)
except SyntaxError:
pass
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
def mod(a,b):
return a%b
def lcm(a,b):
l=math.lcm(a,b)
def hcf(a,b):
h=math.gcd(a,b)
return h
operations={'ADD':add, 'ADDITION':add, 'SUM' :add, 'PLUS':add,
'SUBTRACTION':sub, 'DIFFERENCE':sub , 'MINUS':sub, 'SUBTRACT':sub,
'PRODUCT': mul, 'MULTIPLICATION': mul, 'MULTIPLY': mul,
'DIVISION': div, 'DIV': div, 'DIVIDE': div,
'LCM':lcm, 'HCF':hcf,
' MOD':mod, 'REMAINDER':mod, 'MODULUS':mod}
def findNumbers(t):
l=[ ]
for num in t:
try:
l.append(int(num))
except ValueError:
pass
return l
def audio():
mixer.music.load('music1.mp3')
mixer.music.play()
sr= speech_recognition.Recognizer()
with speech_recognition.Microphone()as m: #is used to avoid exception
try:
sr.adjust_for_ambient_noise(m,duration=0.2)
voice=sr.listen(m)
text=sr.recognize_google(voice)
mixer.music.load('music2.mp3')
mixer.music.play()
text_list=text.split(' ')
for word in text_list:
if word.upper() in operations.keys():
l=findNumbers(text_list)
print(l)
result=operations[word.upper()](l[0],l[1])#function call mul(5.0,6.0)
entryField.delete(0,END)
entryField.insert(END,result)
else:
pass
except:
pass
root=Tk()
root.title('Smart Calculator')
root.config(bg='black')
root.geometry('680x486+100+100')
logoImage=PhotoImage(file='logo.png')
logoLabel=Label(root,image=logoImage,bg='black')
logoLabel.grid(row=0,column=0)
entryField=Entry(root, font=('arial',20, 'bold'), bg='white',fg='black', bd=10,
relief=SUNKEN, width=30)
entryField.grid(row=0,column=0, columnspan=8)
micImage=PhotoImage(file='microphone.png')
micButton=Button(root, image=micImage ,bg='black', activebackground='black'
,command=audio)
micButton.grid(row=0, column=7)
button_text_list = ["C", "CE","√","+","π","cos", "tan", "sin", "1", "2", "3",
"-", "1/x", "cosh", "tanh", "sinh", "4", "5", "6", "*",
chr(8731), "x\u02b8", "x\u00b3", "x\u00b2","7", "8", "9",
chr(247), "log10","ln", "deg", "rad", "e", ".", "0", "%", "=",
"(", ")", "x!"]
rowValue = 1
colValue = 0
for i in button_text_list:
button = Button(root, width=5, height=2, bd=4, relief=SUNKEN, text= i,
bg='gray', fg='black', font = ('arial', 18, 'bold'),
activebackground='white', activeforeground='red',
command=lambda button = i: click(button))
button.grid(row= rowValue, column = colValue, pady=1)
colValue += 1
if(colValue > 7):
rowValue+=1
colValue = 0
root.mainloop()