# Scientific Calculator in Python

Who does not need a calculator in this era? Scientific calculators simply need an hour; having one at our fingertips is helpful. Today we will learn about building a Scientific Calculator in Python. Having a scientific calculator is an absolute must when it comes to a science student’s everyday requirements. Having a scientific calculator on your screen greatly facilitates completing online projects, especially in these days of online learning. Click here to copy the source code.

Have you ever wondered about developing a GUI Scientific Calculator in Python? If not, let’s learn to make one in this article on GUI Scientific Calculator in Python. Here in this article, we will take you through all the concepts that are important and also provide you with the source code for your reference.

## Scientific Calculator in Python: Project Overview

Python GUI Scientific Calculator with Code is a straightforward project created in Python. This project is based on a straightforward GUI and is very simple to comprehend and use. Additionally, the user may easily learn how to execute numerical computations with the help of this project. Like a typical calculator, the project has numbers, operators, and signs. In order to enter any number or simply click on the numbers they desire for the computations, the user needs only click. As a result, the user of this application can utilize a straightforward calculator.

## Features of Scientific Calculator

• All basic operators like addition, multiplication, subtraction, and division.
• Dedicated buttons for sin, cos, tan, and cot.
• Functions for log
• Functions for ratio and exponents.
• Other buttons like Pi, mod, and x!

## Main Code

Now that we know what we are going to develop. Let us start our Scientific Calculator in Python using the Tkinter development journey without wasting a single second. An in-depth explanation along with source code will be definitely a good experience to learn this project.

### Basic library imports and window configuration

``````from tkinter import *
import math
import tkinter.messagebox

root = Tk()
root.title("Scientific Calculator")
root.configure(background = 'red')
root.resizable(width=False, height=False)
root.geometry("480x568+450+90")
calc = Frame(root)
calc.grid()``````

Explanation:
Here we imported some libraries and set up the title for our Scientific Calculator in the Python window. We also used the geometry() function of the Tkinter library to set the size of the window. Besides that, we have also used a grid() function to place each and every box in its calculator at the correct position and designed the overall calc in the form of a grid.

### Class definition for all the Scientific Calculator functions

``````class Calc():
def __init__(self):
self.total=0
self.current=''
self.input_value=True
self.check_sum=False
self.op=''
self.result=False

def numberEnter(self, num):
self.result=False
firstnum=txtDisplay.get()
secondnum=str(num)
if self.input_value:
self.current = secondnum
self.input_value=False
else:
if secondnum == '.':
if secondnum in firstnum:
return
self.current = firstnum+secondnum
self.display(self.current)

def sum_of_total(self):
self.result=True
self.current=float(self.current)
if self.check_sum==True:
self.valid_function()
else:
self.total=float(txtDisplay.get())

def display(self, value):
txtDisplay.delete(0, END)
txtDisplay.insert(0, value)

def valid_function(self):
self.total += self.current
if self.op == "sub":
self.total -= self.current
if self.op == "multi":
self.total *= self.current
if self.op == "divide":
self.total /= self.current
if self.op == "mod":
self.total %= self.current
self.input_value=True
self.check_sum=False
self.display(self.total)

def operation(self, op):
self.current = float(self.current)
if self.check_sum:
self.valid_function()
elif not self.result:
self.total=self.current
self.input_value=True
self.check_sum=True
self.op=op
self.result=False

def Clear_Entry(self):
self.result = False
self.current = "0"
self.display(0)
self.input_value=True

def All_Clear_Entry(self):
self.Clear_Entry()
self.total=0

def pi(self):
self.result = False
self.current = math.pi
self.display(self.current)

def tau(self):
self.result = False
self.current = math.tau
self.display(self.current)

def e(self):
self.result = False
self.current = math.e
self.display(self.current)

def mathPM(self):
self.result = False
self.current = -(float(txtDisplay.get()))
self.display(self.current)

def squared(self):
self.result = False
self.current = math.sqrt(float(txtDisplay.get()))
self.display(self.current)

def cos(self):
self.result = False
self.display(self.current)

def cosh(self):
self.result = False
self.display(self.current)

def tan(self):
self.result = False
self.display(self.current)

def tanh(self):
self.result = False
self.display(self.current)

def sin(self):
self.result = False
self.display(self.current)

def sinh(self):
self.result = False
self.display(self.current)

def log(self):
self.result = False
self.current = math.log(float(txtDisplay.get()))
self.display(self.current)

def exp(self):
self.result = False
self.current = math.exp(float(txtDisplay.get()))
self.display(self.current)

def acosh(self):
self.result = False
self.current = math.acosh(float(txtDisplay.get()))
self.display(self.current)

def asinh(self):
self.result = False
self.current = math.asinh(float(txtDisplay.get()))
self.display(self.current)

def expm1(self):
self.result = False
self.current = math.expm1(float(txtDisplay.get()))
self.display(self.current)

def lgamma(self):
self.result = False
self.current = math.lgamma(float(txtDisplay.get()))
self.display(self.current)

def degrees(self):
self.result = False
self.current = math.degrees(float(txtDisplay.get()))
self.display(self.current)

def log2(self):
self.result = False
self.current = math.log2(float(txtDisplay.get()))
self.display(self.current)

def log10(self):
self.result = False
self.current = math.log10(float(txtDisplay.get()))
self.display(self.current)

def log1p(self):
self.result = False
self.current = math.log1p(float(txtDisplay.get()))
self.display(self.current)``````

Explanation:
In this code section, we used the class to add various functions. At first, we defined the class with the name Calc() and then defined all the functions that we want in want in our Scientific Calculator in Python in the form of a Python function. Apart from that, we have also defined the function to handle the input number entered by the user. Also, to check if the function is valid or not we have defined a separate function in this class. We have included almost all the important functions that are in a normal scientific calculator. In this, we have made use of a math library and various built-in functions in that library. These functions are called when the button relating to it is clicked by the user. The logic of the function is made using the functions provided by the math library.

### To display the text

``````added_value = Calc()

txtDisplay = Entry(calc, font=('Helvetica',20,'bold'),
bg='black',fg='red',
bd=30,width=28,justify=RIGHT)
txtDisplay.insert(0,"0")``````

Explanation:
In this code block, we have defined a variable to store the Calc() class in it. On top of that, in order to display the text box, wherein the user can see the entered values and functions, we have added a text box and customized it according to our needs. To set the size of the textbox we made use of width. Here bd stands for border. We changed the fonts, background, and alignment of the text. We also used the grid() function of the Tkinter library to set its position in a proper way.

### Adding and Arranging the numbers

``````numberpad = "789456123"
i=0
btn = []
for j in range(2,5):
for k in range(3):
btn.append(Button(calc, width=6, height=2,
bg='black',fg='red',
font=('Helvetica',20,'bold'),
btn[i].grid(row=j, column= k, pady = 1)
i+=1``````

Explanation:
Here, in this part of Scientific Calculator in Python, we have defined a variable named “numberpad” that stores all the digits. Now in order to make a separate box for each of the digits, we made use of for loop. This loop helps us to create 9 boxes for 9 digits that will be of the same size and font. Using the for loop helps us to eradicate the need of writing the code 9 times to display the boxes.

### Buttons to handle the Clear and Clear all operation

``````btnClear = Button(calc, text=chr(67),width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold')
).grid(row=1, column= 0, pady = 1)

btnAllClear = Button(calc, text=chr(67)+chr(69),
width=6, height=2,
bg='powder blue',
font=('Helvetica',20,'bold'),
bd=4,
).grid(row=1, column= 1, pady = 1)``````

Explanation:
In this part of the code for Scientific Calculator in Python, the two buttons help the user to handle the operation like clearing a digit or a function. The buttons for Clear All clear everything that is added in the textbox. In this also we have customized the buttons according to our needs.

### Buttons for all operations of the Standard Calculator

``````btnsq = Button(calc, text="\u221A",width=6, height=2,
bg='powder blue', font=('Helvetica',
20,'bold'),
).grid(row=1, column= 2, pady = 1)

bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 3, pady = 1)

btnSub = Button(calc, text="-",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 3, pady = 1)

btnMul = Button(calc, text="x",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 3, pady = 1)

btnDiv = Button(calc, text="/",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 3, pady = 1)

btnZero = Button(calc, text="0",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 0, pady = 1)

btnDot = Button(calc, text=".",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 1, pady = 1)
btnPM = Button(calc, text=chr(177),width=6,
height=2,bg='powder blue', font=('Helvetica',20,'bold'),
).grid(row=5, column= 2, pady = 1)

btnEquals = Button(calc, text="=",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 3, pady = 1)``````

Explanation:
These above variables are for the buttons that are for performing various operations like Multiply, Divide, Subtract, Add etc. These buttons when clicked, call the respective function that is attached to them, thus helping the user in performing the desired operation easily. For the design purpose, we made use of all the properties that are provided by the Button function of the Tkinter library. On top of that, a grid() function is also used somewhere.

### Buttons for Scientific Calculator – Row 1

``````btnPi = Button(calc, text="pi",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 4, pady = 1)

btnCos = Button(calc, text="Cos",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 5, pady = 1)

btntan = Button(calc, text="tan",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 6, pady = 1)

btnsin = Button(calc, text="sin",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 7, pady = 1)``````

Explanation:
In this row, we have added the buttons for the functions of Pi, sin, cos, and tan. This same pattern goes on for the other 4 rows. Also, we have added the functions along with the respective buttons.

### Buttons for Scientific Calculator – Row 2

``````btn2Pi = Button(calc, text="2pi",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 4, pady = 1)

btnCosh = Button(calc, text="Cosh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 5, pady = 1)

btntanh = Button(calc, text="tanh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 6, pady = 1)

btnsinh = Button(calc, text="sinh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 7, pady = 1)``````

### Buttons for Scientific Calculator – Row 3

``````btnlog = Button(calc, text="log",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 4, pady = 1)

btnExp = Button(calc, text="exp",width=6, height=2,
bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 5, pady = 1)

btnMod = Button(calc, text="Mod",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 6, pady = 1)

btnE = Button(calc, text="e",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 7, pady = 1)
``````

### Buttons for Scientific Calculator – Row 4

``````btnlog10 = Button(calc, text="log10",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 4, pady = 1)

btncos = Button(calc, text="log1p",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 5, pady = 1)

btnexpm1 = Button(calc, text="expm1",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 6, pady = 1)

btngamma = Button(calc, text="gamma",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 7, pady = 1)``````

### Buttons for Scientific Calculator – Row 5

``````btnlog2 = Button(calc, text="log2",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 4, pady = 1)

btndeg = Button(calc, text="deg",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 5, pady = 1)

btnacosh = Button(calc, text="acosh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 6, pady = 1)

btnasinh = Button(calc, text="asinh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 7, pady = 1)

lblDisplay = Label(calc, text = "Scientific Calculator",
font=('Helvetica',30,'bold'),
bg='black',fg='red',justify=CENTER)

lblDisplay.grid(row=0, column= 4,columnspan=4)``````

``````def iExit():
"Do you want to exit ?")
if iExit>0:
root.destroy()
return

def Scientific():
root.resizable(width=False, height=False)
root.geometry("944x568+0+0")

def Standard():
root.resizable(width=False, height=False)
root.geometry("480x568+0+0")

Explanation:
In this section, we have the code to make up the Menubar. This will simply come on top of the calculator. We have added multiple options like File and Edit. Moreover, to handle the opening and closing of the Scientific calc window we have defined two new functions namely Standard() and Scientific(). Inside these functions, we have used the resizable and geometry functions of the Tkinter library.

``````filemenu = Menu(menubar, tearoff = 0)

root.mainloop()
``````

Explanation:
Talking about the sub-options in the “File” button there is a feature to switch between “Standard” and “Scientific” Calculator. Inside the “Edit” button there is Copy, Paste andCut option. These options will help to copy the answers, paste other values in this calc and also cut the answer. Whenever the user will click on the Scientific calculator, a window beside the Standard Calc will open up. A separate window helps a user to close and access Scientific calc with more use and thus we decided to code in such a way.

## Complete Code for GUI Scientific Calculator in Python in Tkinter

```from tkinter import *
import math
import tkinter.messagebox

root = Tk()
root.title("Scientific Calculator")
root.configure(background = 'red')
root.resizable(width=False, height=False)
root.geometry("480x568+450+90")
calc = Frame(root)
calc.grid()

class Calc():
def __init__(self):
self.total=0
self.current=''
self.input_value=True
self.check_sum=False
self.op=''
self.result=False

def numberEnter(self, num):
self.result=False
firstnum=txtDisplay.get()
secondnum=str(num)
if self.input_value:
self.current = secondnum
self.input_value=False
else:
if secondnum == '.':
if secondnum in firstnum:
return
self.current = firstnum+secondnum
self.display(self.current)

def sum_of_total(self):
self.result=True
self.current=float(self.current)
if self.check_sum==True:
self.valid_function()
else:
self.total=float(txtDisplay.get())

def display(self, value):
txtDisplay.delete(0, END)
txtDisplay.insert(0, value)

def valid_function(self):
self.total += self.current
if self.op == "sub":
self.total -= self.current
if self.op == "multi":
self.total *= self.current
if self.op == "divide":
self.total /= self.current
if self.op == "mod":
self.total %= self.current
self.input_value=True
self.check_sum=False
self.display(self.total)

def operation(self, op):
self.current = float(self.current)
if self.check_sum:
self.valid_function()
elif not self.result:
self.total=self.current
self.input_value=True
self.check_sum=True
self.op=op
self.result=False

def Clear_Entry(self):
self.result = False
self.current = "0"
self.display(0)
self.input_value=True

def All_Clear_Entry(self):
self.Clear_Entry()
self.total=0

def pi(self):
self.result = False
self.current = math.pi
self.display(self.current)

def tau(self):
self.result = False
self.current = math.tau
self.display(self.current)

def e(self):
self.result = False
self.current = math.e
self.display(self.current)

def mathPM(self):
self.result = False
self.current = -(float(txtDisplay.get()))
self.display(self.current)

def squared(self):
self.result = False
self.current = math.sqrt(float(txtDisplay.get()))
self.display(self.current)

def cos(self):
self.result = False
self.display(self.current)

def cosh(self):
self.result = False
self.display(self.current)

def tan(self):
self.result = False
self.display(self.current)

def tanh(self):
self.result = False
self.display(self.current)

def sin(self):
self.result = False
self.display(self.current)

def sinh(self):
self.result = False
self.display(self.current)

def log(self):
self.result = False
self.current = math.log(float(txtDisplay.get()))
self.display(self.current)

def exp(self):
self.result = False
self.current = math.exp(float(txtDisplay.get()))
self.display(self.current)

def acosh(self):
self.result = False
self.current = math.acosh(float(txtDisplay.get()))
self.display(self.current)

def asinh(self):
self.result = False
self.current = math.asinh(float(txtDisplay.get()))
self.display(self.current)

def expm1(self):
self.result = False
self.current = math.expm1(float(txtDisplay.get()))
self.display(self.current)

def lgamma(self):
self.result = False
self.current = math.lgamma(float(txtDisplay.get()))
self.display(self.current)

def degrees(self):
self.result = False
self.current = math.degrees(float(txtDisplay.get()))
self.display(self.current)

def log2(self):
self.result = False
self.current = math.log2(float(txtDisplay.get()))
self.display(self.current)

def log10(self):
self.result = False
self.current = math.log10(float(txtDisplay.get()))
self.display(self.current)

def log1p(self):
self.result = False
self.current = math.log1p(float(txtDisplay.get()))
self.display(self.current)

txtDisplay = Entry(calc, font=('Helvetica',20,'bold'),
bg='black',fg='red',
bd=30,width=28,justify=RIGHT)
txtDisplay.insert(0,"0")

i=0
btn = []
for j in range(2,5):
for k in range(3):
btn.append(Button(calc, width=6, height=2,
bg='black',fg='red',
font=('Helvetica',20,'bold'),
btn[i].grid(row=j, column= k, pady = 1)
i+=1

btnClear = Button(calc, text=chr(67),width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold')
).grid(row=1, column= 0, pady = 1)

btnAllClear = Button(calc, text=chr(67)+chr(69),
width=6, height=2,
bg='powder blue',
font=('Helvetica',20,'bold'),
bd=4,
).grid(row=1, column= 1, pady = 1)

btnsq = Button(calc, text="\u221A",width=6, height=2,
bg='powder blue', font=('Helvetica',
20,'bold'),
).grid(row=1, column= 2, pady = 1)

bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 3, pady = 1)

btnSub = Button(calc, text="-",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 3, pady = 1)

btnMul = Button(calc, text="x",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 3, pady = 1)

btnDiv = Button(calc, text="/",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 3, pady = 1)

btnZero = Button(calc, text="0",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 0, pady = 1)

btnDot = Button(calc, text=".",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 1, pady = 1)
btnPM = Button(calc, text=chr(177),width=6,
height=2,bg='powder blue', font=('Helvetica',20,'bold'),
).grid(row=5, column= 2, pady = 1)

btnEquals = Button(calc, text="=",width=6,
height=2,bg='powder blue',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 3, pady = 1)

btnPi = Button(calc, text="pi",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 4, pady = 1)

btnCos = Button(calc, text="Cos",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 5, pady = 1)

btntan = Button(calc, text="tan",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 6, pady = 1)

btnsin = Button(calc, text="sin",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=1, column= 7, pady = 1)

btn2Pi = Button(calc, text="2pi",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 4, pady = 1)

btnCosh = Button(calc, text="Cosh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 5, pady = 1)

btntanh = Button(calc, text="tanh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 6, pady = 1)

btnsinh = Button(calc, text="sinh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=2, column= 7, pady = 1)

btnlog = Button(calc, text="log",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 4, pady = 1)

btnExp = Button(calc, text="exp",width=6, height=2,
bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 5, pady = 1)

btnMod = Button(calc, text="Mod",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 6, pady = 1)

btnE = Button(calc, text="e",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=3, column= 7, pady = 1)

btnlog10 = Button(calc, text="log10",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 4, pady = 1)

btncos = Button(calc, text="log1p",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 5, pady = 1)

btnexpm1 = Button(calc, text="expm1",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 6, pady = 1)

btngamma = Button(calc, text="gamma",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=4, column= 7, pady = 1)

btnlog2 = Button(calc, text="log2",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 4, pady = 1)

btndeg = Button(calc, text="deg",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 5, pady = 1)

btnacosh = Button(calc, text="acosh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 6, pady = 1)

btnasinh = Button(calc, text="asinh",width=6,
height=2,bg='black',fg='red',
font=('Helvetica',20,'bold'),
).grid(row=5, column= 7, pady = 1)

lblDisplay = Label(calc, text = "Scientific Calculator",
font=('Helvetica',30,'bold'),
bg='black',fg='red',justify=CENTER)

lblDisplay.grid(row=0, column= 4,columnspan=4)

def iExit():
"Do you want to exit ?")
if iExit>0:
root.destroy()
return

def Scientific():
root.resizable(width=False, height=False)
root.geometry("944x568+0+0")

def Standard():
root.resizable(width=False, height=False)
root.geometry("480x568+0+0")

root.mainloop()```