Scientific GUI calculator using Tkinter in Python
Here we will learn to build a Scientific GUI (Graphical User Interface) calculator using tkinter module in Python.
Python Tkinter
The tkinter package is the standard Python interface to the Tk GUI toolkit.
With this module we can build Python based GUI applications.
Scientific GUI calculator using Tkinter
Program
import math as m
from tkinter import *
#Creating main window
root = Tk()
# Giving title to the window
root.title("Scientific Calculator")
# Function used to display values on to the screen Entry box
def enter(val):
if display.get() == "Invalid Input":
display.delete(0, END)
display.insert(END, val)
# Function executes when '=' button is pressed
# Evaluates the value and gives out the result
def EVAL():
try:
value = eval(display.get())
except:
value = "Invalid Input"
display.delete(0, END)
display.insert(0, value)
# Display Box
display = Entry(root, width=60, borderwidth=5)
display.grid(row=0, column=0, columnspan=5)
# Buttons
# Sine
sin = Button(root, text="sin", padx=23, pady=5, bg='#c2d6d6', command=lambda: enter("m.sin("))
# cosine
cos = Button(root, text="cos", padx=23, pady=5, bg='#c2d6d6', command=lambda: enter("m.cos("))
# tangent
tan = Button(root, text="tan", padx=23, pady=5, bg='#c2d6d6', command=lambda: enter("m.tan("))
# hyperbolic sine
sinh = Button(root, text="sinh", padx=20, pady=5, bg='#c2d6d6', command=lambda: enter("m.sinh("))
# hyperbolic cosine
cosh = Button(root, text="cosh", padx=20, pady=5, bg='#c2d6d6', command=lambda: enter("m.cosh("))
# hyperbolic tangent
tanh = Button(root, text="tanh", padx=20, pady=5, bg='#c2d6d6', command=lambda: enter("m.tanh("))
# square root
sqrt = Button(root, text="√", padx=25, pady=5, bg='#c2d6d6', command=lambda: enter("m.sqrt("))
e = Button(root, text="e", padx=28, pady=5, bg='#c2d6d6', command=lambda: enter("m.e"))
pi = Button(root, text="π", padx=25, pady=5, bg='#c2d6d6', command=lambda: enter("m.pi"))
expo = Button(root, text="X^", padx=22, pady=5, bg='#c2d6d6', command=lambda: enter("**"))
# factorial
fac = Button(root, text="X!", padx=25, pady=7, bg='#c2d6d6', command=lambda: enter("m.factorial("))
log = Button(root, text="log", padx=23, pady=7, bg='#c2d6d6', command=lambda: enter("m.log("))
log10 = Button(root, text="log10", padx=18, pady=7, bg='#c2d6d6', command=lambda: enter("m.log10("))
# Complex numbers
comp = Button(root, text="complex \n (real, imaginary)", padx=22, pady=0, command=lambda: enter("complex("))
p1 = Button(root, text="(", padx=28, pady=5, command=lambda: enter("("))
p2 = Button(root, text=")", padx=27, pady=5, command=lambda: enter(")"))
n0 = Button(root, text="0", padx=28, pady=5, bg='#66ffff', command=lambda: enter(0))
n1 = Button(root, text="1", padx=28, pady=5, bg='#66ffff', command=lambda: enter(1))
n2 = Button(root, text="2", padx=28, pady=5, bg='#66ffff', command=lambda: enter(2))
n3 = Button(root, text="3", padx=28, pady=5, bg='#66ffff', command=lambda: enter(3))
n4 = Button(root, text="4", padx=28, pady=5, bg='#66ffff', command=lambda: enter(4))
n5 = Button(root, text="5", padx=28, pady=5, bg='#66ffff', command=lambda: enter(5))
n6 = Button(root, text="6", padx=28, pady=5, bg='#66ffff', command=lambda: enter(6))
n7 = Button(root, text="7", padx=28, pady=5, bg='#66ffff', command=lambda: enter(7))
n8 = Button(root, text="8", padx=28, pady=5, bg='#66ffff', command=lambda: enter(8))
n9 = Button(root, text="9", padx=28, pady=5, bg='#66ffff', command=lambda: enter(9))
add = Button(root, text="+", padx=26, pady=5, bg='#66ffff', command=lambda: enter('+'))
sub = Button(root, text="-", padx=28, pady=5, bg='#66ffff', command=lambda: enter('-'))
mul = Button(root, text="*", padx=28, pady=5, bg='#66ffff', command=lambda: enter('*'))
div = Button(root, text="/", padx=28, pady=5, bg='#66ffff', command=lambda: enter('/'))
dot = Button(root, text=".", padx=29, pady=5, bg='#66ffff', command=lambda: enter('.'))
clear = Button(root, text="AC", padx=21, pady=5, bg='#ff8080', command=lambda: display.delete(0, END))
cut = Button(root, text="DEL", padx=20, pady=5, bg='#ffb3b3', command=lambda: display.delete(len(display.get())-1, END))
equal = Button(root, text="=", padx=25, pady=5, bg='#99ff99', command=lambda: EVAL())
#Providing position to the buttons
n7.grid(row=1, column=0)
n8.grid(row=1, column=1)
n9.grid(row=1, column=2)
cut.grid(row=1, column=3)
clear.grid(row=1, column=4)
n4.grid(row=2, column=0)
n5.grid(row=2, column=1)
n6.grid(row=2, column=2)
mul.grid(row=2, column=3)
div.grid(row=2, column=4)
n1.grid(row=3, column=0)
n2.grid(row=3, column=1)
n3.grid(row=3, column=2)
add.grid(row=3, column=3)
sub.grid(row=3, column=4)
n0.grid(row=4, column=0)
dot.grid(row=4, column=1)
e.grid(row=4, column=2)
pi.grid(row=4, column=3)
equal.grid(row=4, column=4)
sin.grid(row=5, column=0)
cos.grid(row=5, column=1)
tan.grid(row=5, column=2)
p1.grid(row=5, column=3)
p2.grid(row=5, column=4)
sinh.grid(row=6, column=0)
cosh.grid(row=6, column=1)
tanh.grid(row=6, column=2)
sqrt.grid(row=6, column=3)
expo.grid(row=6, column=4)
fac.grid(row=7, column=0)
log.grid(row=7, column=1)
log10.grid(row=7, column=2)
comp.grid(row=7, column=3, columnspan=2)
root.mainloop()
إرسال تعليق
If you have any query, feel free to ask.