# Scientific Calculator in Python

## Scientific Calculator With Python

Hey coder!!! In this article we will explain step by step how to create Scientific Calculator in Python , using the tinker library of Python.

We all need a scientific calculator sometimes, because now i am doing CSE engineering, so I need this now. Well, we all need it sometimes and we go to buy it. Why don’t we make it ourselves today and that too for free. Without any plastic or environmentally harmful elements, we will make it from our laptop by coding in Python.

In this project, using tkinter and math library of Python.

Lets start the project

First of all we will install Python and download the interpreter, after that we will import some libraries by using the import function.

using this line for import modules in command prompet “pip install <module-name>” .

### Output Preview:-

First of all you have installed all of the necessary packages and tools, you are ready to start creating your projects.

#### Step 1: Import Libraries:-

```import tkinter
import math```

Here, These are some modules which we have to import

#### Step 2: Define the Calculator Functions:-

```def click(val):
# Function to handle button clicks and perform corresponding actions```

This function works by clicking the button of the calculator.

#### Step 3: Create the GUI Window:-

```root = tkinter.Tk()
root.title("Scientific Calculator")
root.geometry("680x486+100+100")
root.config(bg="black")```

#### Step 4: Create 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)
```

A entry field is added to GUI to display the input and output.

#### Step 5: Create Buttons:-

```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!"]```

in this line of code , list the button is added with their respective labels.

#### Step 6: Place Buttons on the GUI:-

```r = 1
c = 0
for i in button_list:
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))
c += 1
if c > 7:
r += 1
c = 0```

In this line of code , Position button on the GUI

#### Step 7: Run the Scientific Calculator in Python:-

`root.mainloop()`

This is the code after all , Tkinter loop function has been used to run the GUI.

### Full code:-

```# Importing the library
import tkinter
import math
# 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θ":
# sin value
elif val == "sinθ":
# tan Value
elif val == "tanθ":
# 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))
# cube root value
elif val == chr(8731):
ans = eval(e) ** (1 / 3)
# 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))
# 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
elif val == "=":
ans = eval(e)
else:
entry.insert("end", val)
return
entry.delete(0, "end")
entry.insert(0, ans)
except SyntaxError:
pass
# Created the object
root = tkinter.Tk()
# Setting the title and geometry
root.title("Scientific Calculator")
root.geometry("680x486+100+100")
# Setting the background color
root.config(bg="black")
# 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))
c += 1
if c > 7:
r += 1
c = 0
# Makes window on loop
root.mainloop()```

## Final Output Scientific Calculator:-

Explanation of the codes written :-

FAQ:-

### Which libraries is used?

Tkinter and maths libraries

### Which function is used to create the button?

We have used `button_list` function to create buttons.

### This Post Has 2 Comments

1. hello!,I really like your writing so a lot! share we keep up a correspondence extra approximately your post on AOL? I need an expert in this house to unravel my problem. May be that is you! Taking a look ahead to see you.

1. Ashutosh Mishra

thanku