This is my code. I was trying the Standard Calculator but the mic image button is not at all displayed. I have not completed my entire code. Standard calculator body is given in def Standard(). Please give me a solution. The icon of the standard calculator is being displayed but the mic button is not shown at all. Still i would like to add Scientific Calculator with almost the same code. Also if anyone has date calculator to find the difference between two dates with calendar date picker, can you send the link. It would be a great help
from tkinter import Tk, ttk, Frame, Button, Entry, Label, messagebox, Toplevel, Menu, END
import math
from pygame import mixer
import speech_recognition
from PIL import Image, ImageTk
def Standard():
mixer.init()
def click(value):
ex = entryField.get() # 789 ex[0:len(ex)-1]
answer = ''
try:
if value == 'C':
ex = ex[0:len(ex) - 1] # 78
entryField.delete(0, END)
entryField.insert(0, ex)
return
elif value == 'AC':
entryField.delete(0, END)
elif value == '√':
answer = math.sqrt(eval(ex))
elif value == 'π':
answer = math.pi
elif value == 'cosθ':
answer = math.cos(math.radians(eval(ex)))
elif value == 'tanθ':
answer = math.tan(math.radians(eval(ex)))
elif value == 'sinθ':
answer = math.sin(math.radians(eval(ex)))
elif value == '2π':
answer = 2 * math.pi
elif value == 'cosh':
answer = math.cosh(eval(ex))
elif value == 'tanh':
answer = math.tanh(eval(ex))
elif value == 'sinh':
answer = math.sinh(eval(ex))
elif value == chr(8731):
answer = eval(ex) ** (1 / 3)
elif value == 'x\u02b8': # 7**2
entryField.insert(END, '**')
return
elif value == 'x\u00B3':
answer = eval(ex) ** 3
elif value == 'x\u00B2':
answer = eval(ex) ** 2
elif value == 'ln':
answer = math.log2(eval(ex))
elif value == 'deg':
answer = math.degrees(eval(ex))
elif value == "rad":
answer = math.radians(eval(ex))
elif value == 'e':
answer = math.e
elif value == 'log₁₀':
answer = math.log10(eval(ex))
elif value == 'x!':
answer = math.factorial(ex)
elif value == chr(247): # 7/2=3.5
entryField.insert(END, "/")
return
elif value == '=':
answer = eval(ex)
else:
entryField.insert(END, value)
return
entryField.delete(0, END)
entryField.insert(0, answer)
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)
return l
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:
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]) #mul(5.0,6.0)
entryField.delete(0,END)
entryField.insert(END,result)
else:
pass
except:
pass
win = Toplevel()
win.title('Smart Calculator')
win.config(bg='Cornflower Blue')
win.resizable(width=False, height=False)
win.geometry('394x477+100+100')
p = ImageTk.PhotoImage(file = 'logo.ico.png')
win.iconphoto(False, p)
entryField = Entry(win, font=('times new roman', 18, 'bold'), bg='grey80', fg='black', bd=10, width=18, )
entryField.grid(row=0, column=0, columnspan=5, padx = 4)
micImage = ImageTk.PhotoImage(file = 'mic1.png')
micButton = Button(win, text = audio, bd=0, bg='Cornflower Blue', activebackground='dodger Blue4',command=audio)
micButton.grid(row=0, column=0)
button_text_list = ["C", "AC", "√", "+", "π", "cosθ", "tanθ", "sinθ",
"1", "2", "3", "-", "2π", "cosh", "tanh", "sinh",
"4", "5", "6", "*", chr(8731), "x\u02b8", "x\u00B3", "x\u00B2",
"7", "8", "9", chr(247), "etn", "deg", "rad", "e",
".", "0", "%", "=", "log₁₀", "(", ")", "x!"]
lblDisplay = Label(win, text = "SCIENTIFIC CALCULATOR",font=('elephant',18,),
bg='Cornflower Blue',fg='White')
lblDisplay.grid(row=0, column= 4,columnspan=4)
rowvalue = 1
columnvalue = 0
for i in button_text_list:
if i == "C":
button = Button(win, width=6, height=2, bd=2, text=i, bg='orange', fg='black',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i == "AC":
button = Button(win, width=6, height=2, bd=2, text=i, bg='orange', fg='black',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in "()":
button = Button(win, width=6, height=2, bd=2, text=i, bg='Burlywood', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in "0123456789":
button = Button(win, width=6, height=2, bd=2, text=i, bg='Light Yellow', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["cosθ", "tanθ", "sinθ", "cosh", "tanh", "sinh"]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='khaki', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in [ chr(8731), "x\u02b8", "x\u00B3", "x\u00B2"]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='spring green3', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["π", "2π"]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='orchid1', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["deg", "rad"]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='firebrick1', fg='white',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["log₁₀","ln"]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='Light Cyan2', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["+","-","*",chr(247),"%","√","."]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='sienna', fg='white',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i in ["="]:
button = Button(win, width=6, height=2, bd=2, text=i, bg='Light Pink', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i == "x!":
button = Button(win, width=6, height=2, bd=2, text=i, bg='tomato', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
elif i == "e":
button = Button(win, width=6, height=2, bd=2, text=i, bg='cadet blue', fg='grey20',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
else :
button = Button(win, width=6, height=2, bd=2, text=i, bg='grey8', fg='white',
font=('garamond', 18, 'bold'), activebackground='cornflower blue', command=lambda button=i: click(button))
button.grid(row=rowvalue, column=columnvalue,padx = 2, pady=2)
columnvalue += 1
if columnvalue > 7:
rowvalue += 1
columnvalue = 0
# Here are the fucntions for ManuBar.
def iExit():
iExit = tk.messagebox.askyesno("Smart Calculator","Do you want to exit ?")
if iExit>0:
win.destroy()
return
menubar = Menu(win)
# ManuBar 1 :
filemenu = Menu(menubar, tearoff = 0)
menubar.add_cascade(label = 'File', menu = filemenu)
filemenu.add_command(label = "Exit", command = iExit)
root = Tk()
root.geometry("300x300")
notebook = ttk.Notebook(root)
notebook.pack(pady=10, expand=True)
# create frames
tab1 = ttk.Frame(notebook, width=400, height=280)
tab2 = ttk.Frame(notebook, width=400, height=280)
tab1.pack(fill='both', expand=True)
tab2.pack(fill='both', expand=True)
# add frames to notebook
notebook.add(tab1, text='Calculators')
notebook.add(tab2, text='Unit Converters')
cl1 = Label(tab1, text = "CALCULATORS", font= ("Stencil", 14), justify = 'center', bg = "Cornflower Blue",fg = "Grey4")
cl1.place(relx = '0.2', rely = '0.075')
cb1 = Button(tab1, text = "Standard", font = ("Garamond",14), width = 10,bg = 'Burlywood', command = Standard)
cb1.place(relx = '0.05', rely = '0.25')
cb2 = Button(tab1, text = "Scientific", font = ("Garamond",14), width = 10,bg = 'Burlywood')
cb2.place(relx = '0.45', rely = '0.25')
cb3 = Button(tab1, text = "Age", font = ("Garamond",14), width = 10,bg = 'Burlywood')
cb3.place(relx = '0.05', rely = '0.45')
cb4 = Button(tab1, text = "Date", font = ("Garamond",14), width = 10,bg = 'Burlywood')
cb4.place(relx = '0.45', rely = '0.45')
cb5 = Button(tab1, text = "BMI", font = ("Garamond",14), width = 10,bg = 'Burlywood')
cb5.place(relx = '0.05', rely = '0.65')
ul2 = Label(tab2, text = "UNIT CONVERTERS", font= ("Stencil", 14), justify = 'center', bg = "Cornflower Blue",fg = "Grey4")
ul2.place(relx = '0.2', rely = '0.075')
ub6 = Button(tab2, text = "Length", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub6.place(relx = '0.05', rely = '0.25')
ub7 = Button(tab2, text = "Mass", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub7.place(relx = '0.45', rely = '0.25')
ub8 = Button(tab2, text = "Volume", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub8.place(relx = '0.05', rely = '0.45')
ub9 = Button(tab2, text = "Temperature", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub9.place(relx = '0.45', rely = '0.45')
ub10 = Button(tab2, text = "Time", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub10.place(relx = '0.05', rely = '0.65')
ub11 = Button(tab2, text = "Digital", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub11.place(relx = '0.45', rely = '0.65')
ub12 = Button(tab2, text = "Currency", font = ("Garamond",14), width = 10,bg = 'Burlywood')
ub12.place(relx = '0.05', rely = '0.85')
root.mainloop()