Python 3 Tkinter Scientific Calculator GUI Script App Using Math Module Full Project For Beginners

Welcome folks today in this blog post we will be building a `tkinter scientific calculator` using tkinter in python 3. All the full source code of the application is shown below.

Get Started

In order to get started you need to make an `app.py` file and copy paste the following code

`app.py`

``````//Enter Your Code Here...

from tkinter import *
import math
import parser
import tkinter.messagebox

root = Tk()
root.title("Calculator")
root.configure(background="gray60")
root.resizable(width=False,height=False)
root.geometry("480x568+0+0")

calc = Frame(root)
calc.grid()

#----------------------------------------------------------------Calc Class and methods-----------------------------------------------------------

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

def numberEnter(self,num):
self.result = False
first_num = txtDisplay.get()
second_num = str(num)

if self.ip_val:
self.current = second_num
self.ip_val = False
else:
if second_num == '.':
if second_num in first_num:
return
self.current = first_num + second_num
self.display(self.current)

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

def valid_function(self):
self.total += self.current
elif self.op == 'sub':
self.total -= self.current
elif self.op == 'mul':
self.total *= self.current
elif self.op == 'div':
self.total /= self.current
elif self.op == 'mod':
self.total %= self.current

self.ip_val = 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.ip_val = True
self.check_sum = True
self.op = op
self.result = False

#------Manipulation of display

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

def clear_entry(self):
self.result = False
self.current = '0'
self.display(0)
self.ip_val = True

def clear_all(self):
self.clear_entry()
self.total = 0

#--------Functions for standard constants

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

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

#--------Functions for calculation

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

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

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

def log(self):
self.result = False
self.current = math.log10(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 exp(self):
self.result = False
self.display(self.current)

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

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

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

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

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

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

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

self.result = False
self.display(self.current)

res = Calc()

#----------------------------------------------------------------Build Grid-----------------------------------------------------------------------

txtDisplay = Entry(calc,font=('arial',20,'bold'),bd=30,bg='gray60',width=28,justify=RIGHT)
txtDisplay.insert(0,'0')

i=0
btn = []

for j in range(2,5):
for k in range(3):
btn[i]['command'] = lambda x = numpad [i]:res.numberEnter(x)
i+=1

#Standard Calculator

#Scientific

def Exit():
if tkinter.messagebox.askyesno("Calculator","Confirm if you want to quit") >0 :
root.destroy()
return

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

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