Skip to content

Instantly share code, notes, and snippets.

@enjis
Last active August 2, 2018 11:25
Show Gist options
  • Save enjis/e2d16c4e3d4b606c46bf2afb5440ea76 to your computer and use it in GitHub Desktop.
Save enjis/e2d16c4e3d4b606c46bf2afb5440ea76 to your computer and use it in GitHub Desktop.
This calculator has almost all important scientific and mathematical functions used in engineering.It has also an additional window for matrix multiplication
from tkinter import*
from tkinter import ttk
from PIL import ImageTk, Image
import math
import webbrowser
import numpy as np
def btntype(numop):
global equation
equation = equation + str(numop)
text_Input.set(equation)
def btnclrdisplay():
global equation
equation = ""
text_Input.set("")
text_Input1.set("")
def reciprocal():
global equation
try:
equation = 1/int(equation)
text_Input1.set(equation)
except ValueError:
text_Input.set("(*_*) U forgot to enter a number first")
def cuberoot():
global equation
try:
x = int(equation)
if 0 <= x:
text_Input1.set(x ** (1. / 3.))
else:
text_Input1.set(-(-x) ** (1. / 3.))
except ValueError:
text_Input.set("(*_*) U forgot to enter a number first")
def btnequals():
global equation
try:
sumup = str(eval(equation))
text_Input1.set(sumup)
except SyntaxError:
text_Input.set("Math Error")
except NameError:
text_Input.set("NOT VALID...use ↑ button ")
def up():
global equation
equation = text_Input1.get()
text_Input.set(equation)
text_Input1.set("")
def backspace():
global equation
try:
l = list(equation)
l.pop()
equation = ''.join(l)
text_Input.set(equation)
except IndexError:
NONE
def scient(t):
global equation
equation = txtDisplay.get()
try:
if t == 1:
text_Input1.set(math.sqrt(float(equation)))
elif t == 0:
text_Input1.set(10**(float(equation)))
elif t == 2:
text_Input1.set(math.gamma(int(equation)))
elif t == 3:
text_Input1.set(math.factorial(int(equation)))
elif t == 4:
text_Input1.set(math.sin(float(equation)))
elif t == 5:
text_Input1.set(math.cos(float(equation)))
elif t == 6:
text_Input1.set(math.tan(float(equation)))
elif t == 7:
text_Input1.set(math.log(float(equation)))
elif t == 8:
text_Input1.set(math.log10(float(equation)))
elif t == 9:
text_Input1.set(math.log2(float(equation)))
elif t == 10:
text_Input1.set(math.asinh(float(equation)))
elif t == 11:
text_Input1.set(math.acosh(float(equation)))
elif t == 12:
text_Input1.set(math.atanh(float(equation)))
elif t == 13:
text_Input1.set(math.fabs(float(equation)))
elif t == 14:
text_Input1.set(math.sinh(float(equation)))
elif t == 15:
text_Input1.set(math.cosh(float(equation)))
elif t == 16:
text_Input1.set(math.tanh(float(equation)))
elif t == 17:
text_Input1.set(math.asin(float(equation)))
elif t == 18:
text_Input1.set(math.acos(float(equation)))
elif t == 19:
text_Input1.set(math.atan(float(equation)))
elif t == 20:
text_Input1.set(math.degrees(float(equation)))
elif t == 21:
text_Input1.set(math.radians(float(equation)))
except ValueError:
if t<=3 or t==7 or t==8 or t==9 or t==10 or t==13 or t==19:
text_Input.set(" (*_*) U forgot to enter a number first ")
elif t == 21:
text_Input.set(" (+_+) enter the angle in degrees")
elif t == 12:
text_Input.set(" ('v') INVALID INPUT..Please enter a number in (-1,1)")
elif t == 11:
text_Input.set(" ('v') INVALID INPUT..Please enter a number in [1,inf)")
elif t==17 or t==18:
text_Input.set(" ('v') INVALID INPUT..Please enter a number in [1,-1]")
else:
text_Input.set(" (*v*) enter the angle in radians")
def scient1(s):
global equation, a
try:
a = equation.split(',')
x = a[0]
y = [i for i in a[1] if i.isdigit()]
y = ''.join(y)
if s == 0:
text_Input1.set(math.log(float(x),float(y)))
if s ==1:
text_Input1.set(math.pow(float(x),float(y)))
if s == 2:
text_Input1.set(math.pow(float(x),(1/float(y))))
except IndexError:
text_Input.set("First enter values of x and y in x,y format")
def create_menuwindow(n):
window = Toplevel(cal)
if n == 1:
gslabel = Label(window, text=f)
gslabel.pack()
if n == 2:
abtlabel = Label(window, image=aboutphoto)
abtlabel.pack()
def createmat_window():
def option_select_m(event):
global m
m = int(m_var.get())
def option_select_n(event):
global n
n = int(n_var.get())
def option_select_p(event):
global p
p = int(p_var.get())
def option_select_q(event):
global q
q = int(q_var.get())
def entry():
global m, n, d, e
nonlocal c, en, ent
try:
if c > 0:
for x in range(d):
en[x].destroy()
for x in range(e):
ent[x].destroy()
c = 0
x = 0
d = m * n
e = p * q
for i in range(m):
for j in range(n):
if x < d:
en.append("")
en[x] = Text(frame2_l,height=2,width=4) # ,textvariable=ent['ent'+ str(x)])
en[x].grid(row=i, column=j)
x += 1
x = 0
for i in range(p):
for j in range(q):
if x < e:
ent.append("")
ent[x] = Text(frame2_r,height=2,width=4) # ,textvariable=ent['ent'+ str(x)])
ent[x].grid(row=i, column=j)
x += 1
c += 1
except NameError:
resultl.config(text="First select rows and columns of\nmatrix A and matrix B ")
def result():
global m,n,p,q,u,v,result, mt_a, mt_b
try:
mt_a = [en[x].get("1.0","end-1c") for x in range(d)]
mt_b = [ent[x].get("1.0","end-1c") for x in range(e)]
if n != p:
resultl.config(text="Multiplication is not feasible\nMAT A columns and MAT B rows must be equal.")
else:
u = np.array(mt_a).reshape(m,n).astype(np.float) # for taking multiple inputs at the same time
v = np.array(mt_b).reshape(p, q).astype(np.float)
result = np.zeros([m, q])
for i in range(len(u)):
for j in range(len(v[0])):
for k in range(len(v)):
result[i][j] += u[i][k] * v[k][j]
a_s = np.array2string(result, precision=2, separator=', ', max_line_width=150)
resultl.config(text=a_s[:])
except (NameError, IndexError, ValueError):
resultl.config(text="First select rows and columns of\nmatrix A and matrix B\nand enter each element of the matrices ")
mat = Toplevel(cal)
mat.geometry('{}x{}'.format(800, 600))
mat.resizable(0,0)
frame1 = Frame(mat,bg='steel blue',width=500,height=100)
frame1.pack(fill=BOTH, expand=1)
choices = ['Select',1, 2, 3, 4, 5, 6, 7]
m=0
n=0
c=0
en = []
ent = []
row_a= Label(frame1, text='MAT A\nROWS',fg='white',bg='steel blue',font=1)
row_a.pack(side=LEFT,padx=10,pady=10)
pop1 = ttk.OptionMenu(frame1, m_var, *choices, command=option_select_m,)
pop1.pack(side=LEFT)
pop1.config(width=5)
column_a = Label(frame1, text='MAT A\nCOLUMNS',fg='white',bg='steel blue',font=1)
column_a.pack(side=LEFT,padx=10,pady=10)
pop2 = ttk.OptionMenu(frame1, n_var, *choices, command=option_select_n)
pop2.pack(side=LEFT)
pop2.config(width=5)
row_b = Label(frame1, text='MAT B\nROWS',fg='white',bg='steel blue',font=1)
row_b.pack(side=LEFT,padx=10,pady=10)
pop3 = ttk.OptionMenu(frame1, p_var, *choices, command=option_select_p)
pop3.pack(side=LEFT)
pop3.config(width=5)
column_b = Label(frame1, text='MAT B\nCOLUMNS',fg='white',bg='steel blue',font=1)
column_b.pack(side=LEFT,padx=10,pady=10)
pop4 = ttk.OptionMenu(frame1, q_var, *choices, command=option_select_q)
pop4.pack(side=LEFT)
pop4.config(width=5)
update_btn = Button(frame1, text="Enter numbers",fg='white',bg='#6a8b98',bd='5',font=1,padx=2,pady=2, command=entry)
update_btn.pack(side=BOTTOM, padx=10, pady=10)
frame2 = Frame(mat, bg='#c2bfba', width=600, height=300)
frame2.pack(fill=BOTH, )
frame2_l = Frame(frame2, bg='#c2bfba', width=500, height=300)
frame2_l.place(x=80,y=20)
frame2_m = Frame(frame2, bg='#c2bfba', width=200, height=300)
frame2_m.place(x=380,y=20)
frame2_r = Frame(frame2, bg='#c2bfba', width=500, height=300)
frame2_r.place(x=420,y=20)
frame3 = Frame(mat, bg='#00a3aa', width=500, height=100)
frame3.pack(fill=BOTH, expand=1)
result_btn = Button(frame3,bg='#5a65ab',fg='white',bd=5, text="RESULT",font='bold', command=result)
result_btn.pack()
frame4 = Frame(mat, bg='#00a3aa', width=500, height=100)
frame4.pack(fill=BOTH, expand=1)
resultl = Label(frame4,bg='#00a3aa',bd=10,font=5)
resultl.pack()
resultl.config(height=20)
frame5 = Frame(mat, bg='#00a3aa', width=500, height=30)
frame5.pack(fill=BOTH, expand=1)
cal = Tk() #TOP LEVEL WINDOW( ROOT_WINDOW )
cal.title("calculator")
cal.resizable(0,0)
equation = ""
y = ""
a = ""
text_Input = StringVar()
text_Input1 = StringVar()
m_var = StringVar() #for matrix entry
n_var = StringVar()
p_var = StringVar()
q_var = StringVar()
v = IntVar() # for radiobutton
f = open("gettingStarted.txt", "r").readlines()
# f = list(filter(lambda x: x is not'{'or'}',f ))
# f = str(f)
# f.replace('{','')
aboutphoto = ImageTk.PhotoImage(Image.open('flowers.jpg'))
menu = Menu(cal)
cal.config(menu=menu)
hlpMenu = Menu(menu)
menu.add_cascade(label="Help", menu=hlpMenu)
hlpMenu.add_command(label="Getting Started",command=lambda:create_menuwindow(1))
hlpMenu.add_command(label="Report Problem",command=lambda: webbrowser.open_new(r"https://mail.google.com/mail/?view=cm&fs=1&to=singhanjalisingh93@gmail.com&su=Reporting a bug"))
hlpMenu.add_command(label="About",command=lambda: create_menuwindow(2))
txtDisplay = Entry(cal, font=('arial', 18, 'bold'), textvariable=text_Input, bd=20, width=65,
bg="turquoise1", justify='right')
txtDisplay.grid(row=0,column=2,columnspan=20)
txtDisplay1 = Entry(cal, font=('arial', 18, 'bold'), textvariable=text_Input1, bd=20, width=65,
bg="steel blue", justify='right')
txtDisplay1.grid(row=1,column=2,columnspan=20)
rad2deg = Radiobutton(cal, text='degree', variable=v, value=1, command=lambda: scient(20)).grid(row=0,column=0)
deg2rad = Radiobutton(cal, text='rad', variable=v, value=2, command=lambda: scient(21)).grid(row=0,column=1)
up = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'), # cal is master
text=" ↑ ", bg="turquoise1", command=up).grid(row=1, columnspan=2,sticky='s')
sinh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'), # cal is master
text=" sinh ", bg="honeydew3", command=lambda: scient(14)).grid(row=2, column=0)
cosh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" cosh ", bg="honeydew3", command=lambda: scient(15)).grid(row=2, column=1)
tanh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" tanh ", bg="honeydew3", command=lambda: scient(16)).grid(row=2, column=2)
Exp = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" EXP ", bg="honeydew3", command=lambda: scient(0)).grid(row=2, column=3)
lbrace = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ( ", bg="honeydew3", command=lambda: btntype('(')).grid(row=2, column=4)
rbrace = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ) ", bg="honeydew3", command=lambda: btntype(')')).grid(row=2, column=5)
btnbkspace = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="←", bg="honeydew3", command=backspace).grid(row=2, column=6)
btnclear = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="C", bg="red", command=btnclrdisplay).grid(row=2, column=7)
btnsqroot = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" 2√ ", bg="honeydew3", command=lambda: scient(1)).grid(row=2, column=10)
btndecpoint = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=".", bg="honeydew3", command=lambda: btntype(".")).grid(row=2, column=8)
btncomma = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ,", bg="honeydew3", command=lambda: btntype(',')).grid(row=2, column=9)
btnasnh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="asinh ", bg="honeydew3", command=lambda: scient(10)).grid(row=3, column=0)
btnacsh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="acosh ", bg="honeydew3", command=lambda: scient(11)).grid(row=3, column=1)
btnatanh = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="atanh ", bg="honeydew3", command=lambda: scient(12)).grid(row=3, column=2)
btnln = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ln ", bg="honeydew3", command=lambda: scient(7)).grid(row=3, column=3)
btnlog = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="log ", bg="honeydew3", command=lambda: scient(8)).grid(row=3, column=4)
btnlog2 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="log2x", bg="honeydew3", command=lambda: scient(9)).grid(row=3, column=5)
btn7 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="7", bg="honeydew3", command=lambda: btntype(7)).grid(row=3, column=6)
btn8 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="8", bg="honeydew3", command=lambda: btntype(8)).grid(row=3, column=7)
btn9 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="9", bg="honeydew3", command=lambda: btntype(9)).grid(row=3, column=8)
Addition = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="+", bg="honeydew3", command=lambda: btntype("+")).grid(row=3, column=9)
btnrem = Button(cal, padx=16, bd=8, fg="black",font=('arial',15, 'bold'),
text=" % ", bg="honeydew3", command=lambda: btntype("%")).grid(row=3, column=10)
btnpi = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" π ", bg="honeydew3", command=lambda: btntype(math.pi)).grid(row=4, column=0)
btne = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" e ", bg="honeydew3", command=lambda: btntype(math.e)).grid(row=4, column=1)
fact = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" n! ", bg="honeydew3", command=lambda: scient(3)).grid(row=4, column=2)
btnlyx = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="logxbasey", bg="honeydew3", command=lambda: [btntype("(logxbasey)"),scient1(0)]).grid(row=4, column=3)
btnepx = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="e^x", bg="honeydew3", command=lambda: scient(3)).grid(row=4, column=4)
btn10px = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" 10^x", bg="honeydew3", command=lambda: scient(3)).grid(row=4, column=5)
btn4 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="4", bg="honeydew3", command=lambda: btntype(4)).grid(row=4, column=6)
btn5 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="5", bg="honeydew3", command=lambda: btntype(5)).grid(row=4, column=7)
btn6 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="6", bg="honeydew3", command=lambda: btntype(6)).grid(row=4, column=8)
Substraction = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="-", bg="honeydew3", command=lambda: btntype("-")).grid(row=4, column=9)
btnreciproc = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" 1/x ", bg="honeydew3", command=reciprocal).grid(row=4, column=10)
btnsin = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" sin ", bg="honeydew3", command=lambda: scient(4)).grid(row=5, column=0)
btncos = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" cos ", bg="honeydew3", command=lambda: scient(5)).grid(row=5, column=1)
btntan = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" tan ", bg="honeydew3", command=lambda: scient(6)).grid(row=5, column=2)
btnxpy = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" x^y ", bg="honeydew3", command=lambda: [btntype('(x^y)'),scient1(1)]).grid(row=5, column=3)
btnxp3 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ^3 ", bg="honeydew3", command=lambda: btntype("**3")).grid(row=5, column=4)
btnsquare = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" ^2 ", bg="honeydew3", command=lambda: btntype("**2")).grid(row=5, column=5)
btn1 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="1", bg="honeydew3", command=lambda: btntype(1)).grid(row=5, column=6)
btn2 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="2", bg="honeydew3", command=lambda: btntype(2)).grid(row=5, column=7)
btn3 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="3", bg="honeydew3", command=lambda: btntype(3)).grid(row=5, column=8)
Multiplication = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="*", bg="honeydew3", command=lambda: btntype("*")).grid(row=5, column=9)
Matrix = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="MAT", bg="honeydew3", command=createmat_window)
Matrix.grid(row=5, column=10)
arcsin = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="arcsin", bg="honeydew3", command=lambda: scient(17)).grid(row=6, column=0)
arccos = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="arccos", bg="honeydew3", command=lambda: scient(18)).grid(row=6, column=1)
arctan = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="arctan", bg="honeydew3", command=lambda: scient(19)).grid(row=6, column=2)
yroot = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" y√x ", bg="honeydew3", command=lambda: [btntype('(xrooty)'),scient1(2)]).grid(row=6, column=3)
cuberoot = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" 3√ ", bg="honeydew3", command=cuberoot).grid(row=6, column=4)
mod = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" |x| ", bg="honeydew3", command=lambda: scient(13)).grid(row=6, column=5)
btneq = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="=", bg="#53ea5e", command=btnequals).grid(row=6, column=6,columnspan=2,sticky='ew')
btn0 = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="0", bg="honeydew3", command=lambda: btntype(0)).grid(row=6, column=8)
Division = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text="/", bg="honeydew3", command=lambda: btntype("/")).grid(row=6, column=9)
btngamma = Button(cal, padx=16, bd=8, fg="black", font=('arial', 15, 'bold'),
text=" Γ ", bg="honeydew3", command=lambda: scient(2)).grid(row=6, column=10)
cal.mainloop() # application entered in infinite loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment