Math Calculator
Math Calculator
In this project, we build up the scientific calculator using the tkinter library of Python. It is the standard GUI library for Python. With its help, we prepared the
GUI for the project, and to add the functionalities of the scientific calculator, we used a math module
Explanation:
The first step is importing all the necessary libraries using the “import” keyword. For the latest version, the tkinter module comes under the future module.
So first, we have to install the future module with the help of the command “pip install <module-name>”. The future module is a built-in module in Python
that inherits new features which is available in the latest Python versions. With “future. moves” we will import the tkinter module.
In the next step, to create an object of the tkinter frame will use “.Tk()”.With this, we will also set the title and geometry of our tkinter application. To set the
background color for the project we will use “.config(bg=<color name>)”. And so, with that, we have set black as the background color for this project.
We will also add an image by the corner to give it a creative aspect. With the help of “.PhotoImage(file=<filename>)”, we will load the picture, by using
the “.Label()” we will make the label widget for this, and “.grid(row=,column= )” will help to get it placed well on the window.
font – The font style in which we want the information to get entered
Finally, with the help of “.grid()” we will set the position for the entry widget.
# To provide functionalities
def click(val):
e = entry.get() # getting the value
ans = " "
try:
# To clear the last inserted text
if val == "C":
e = e[0:len(e) - 1] # deleting the last entered value
entry.delete(0, "end")
entry.insert(0, e)
return
# To delete everything
elif val == "CE":
entry.delete(0, "end")
# Square root
elif val == "√":
ans = math.sqrt(eval(e))
# pi value
elif val == "π":
ans = math.pi
# cos value
elif val == "cosθ":
ans = math.cos(math.radians(eval(e)))
# sin value
elif val == "sinθ":
ans = math.sin(math.radians(eval(e)))
# tan Value
elif val == "tanθ":
ans = math.tan(math.radians(eval(e)))
# 2π value
elif val == "2π":
ans = 2 * math.pi
# cosh value
elif val == "cosh":
ans = math.cosh(eval(e))
# sinh value
elif val == "sinh":
ans = math.sinh(eval(e))
# tanh value
elif val == "tanh":
ans = math.tanh(eval(e))
# x to the power y
elif val == "x\u02b8":
entry.insert("end", "**")
return
# cube value
elif val == "x\u00B3":
ans = eval(e) ** 3
# square value
elif val == "x\u00B2":
ans = eval(e) ** 2
# ln value
elif val == "ln":
ans = math.log2(eval(e))
# deg value
elif val == "deg":
ans = math.degrees(eval(e))
# radian value
elif val == "rad":
ans = math.radians(eval(e))
# e value
elif val == "e":
ans = math.e
# log10 value
elif val == "log10":
ans = math.log10(eval(e))
# factorial value
elif val == "x!":
ans = math.factorial(eval(e))
# division operator
elif val == chr(247):
entry.insert("end", "/")
return
else:
entry.insert("end", val)
return
entry.delete(0, "end")
entry.insert(0, ans)
except SyntaxError:
pass
# Created the object
root = tkinter.Tk()
# Entry field
entry = tkinter.Entry(root, font=("arial", 20, "bold"), bg="black", fg="white",
bd=10, width=30)
entry.grid(row=0, column=0, columnspan=8)
# buttons list
button_list = ["C", "CE", "√", "+", "π", "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), "ln", "deg",
"rad", "e", "0", ".", "%", "=", "log10", "(", ")", "x!"]
r=1
c=0
# Loop to get the buttons on window
for i in button_list:
# Buttons
button = tkinter.Button(root, width=5, height=2, bd=2, text=i, bg="black",
fg="white",
font=("arial", 18, "bold"), command=lambda button=i:
click(button))
button.grid(row=r, column=c, pady=1)
c += 1
if c > 7:
r += 1
c=0