Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import*
- import math
- import parser
- import tkinter.messagebox
- import tkinter.messagebox as tmsg
- import os
- import time
- from math import sin,cos,tan,log
- from tkinter.filedialog import askopenfilename,askopenfile,asksaveasfile,asksaveasfilename
- root = Tk()
- root.title("Scientific Calculator")
- root.configure(background="Powder blue")
- root.resizable(width=False, height=False)
- root.geometry("480x624+20+20")
- calc = Frame(root)
- calc.grid()
- #=============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 valid_function(self):
- if self.op=="add":
- 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
- if self.op=="inv":
- self.total=1/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 tanh(self):
- self.reult=False
- self.current=math.tanh(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def tan(self):
- self.reult=False
- self.current=math.tan(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def sinh(self):
- self.reult=False
- self.current=math.sinh(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def sin(self):
- self.reult=False
- self.current=math.sin(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def log(self):
- self.reult=False
- self.current=math.log(float(txtDisplay.get()))
- self.display(self.current)
- def exp(self):
- self.reult=False
- self.current=math.exp(float(txtDisplay.get()))
- self.display(self.current)
- def mathsPM(self):
- self.reult=False
- self.current=-(float(txtDisplay.get()))
- self.display(self.current)
- def squared(self):
- self.reult=False
- self.current=math.sqrt(float(txtDisplay.get()))
- self.display(self.current)
- def cos(self):
- self.reult=False
- self.current=math.cos(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def cosh(self):
- self.reult=False
- self.current=math.cosh(math.radians(float(txtDisplay.get())))
- self.display(self.current)
- def display(self, value):
- txtDisplay.delete(0, END)
- txtDisplay.insert(0, value)
- def pi(self):
- self.reult=False
- self.current=math.pi
- self.display(self.current)
- def tau(self):
- self.reult=False
- self.current=math.tau
- self.display(self.current)
- def e(self):
- self.reult=False
- self.current=math.e
- 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)
- added_value=Calc()
- #================Display================================================================================================
- txtDisplay = Entry(calc, relief=SUNKEN, font=('arial', 20, 'bold'), bg="powder blue", bd=30, width=28, justify=RIGHT)
- txtDisplay.grid(row=0, column=0, columnspan=4, pady=1)
- txtDisplay.insert(0, "0")
- #==================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, font=('arial', 20, 'bold'), bd=4, text=numberpad[i]))
- btn[i].grid(row=j, column=k, pady=1)
- btn[i]["command"]=lambda x=numberpad[i]: added_value.numberEnter(x)
- i+=1
- #========================Standard Function==============================================================================================================================================
- btnClear=Button(calc, text=chr(67), width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=added_value.Clear_Entry).grid(row=1, column=0, pady=1)
- btnAllClear=Button(calc, text=chr(65)+chr(67), width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=added_value.all_Clear_Entry).grid(row=1, column=1, pady=1)
- btnSq=Button(calc, text="√", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=added_value.squared).grid(row=1, column=2, pady=1)
- btnAdd=Button(calc, text="+", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.operation("add")).grid(row=1, column=3, pady=1)
- btnSub=Button(calc, text="-", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.operation("sub")).grid(row=2, column=3, pady=1)
- btnMult=Button(calc, text="×", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.operation("multi")).grid(row=3, column=3, pady=1)
- btnDiv=Button(calc, text=chr(247), width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.operation("divide")).grid(row=4, column=3, pady=1)
- btnZero=Button(calc, text="0", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.numberEnter(0)).grid(row=5, column=0, pady=1)
- btnDot=Button(calc, text=".", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=lambda:added_value.numberEnter(".")).grid(row=5, column=1, pady=1)
- btnPM=Button(calc, text=chr(177), width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=added_value.mathsPM).grid(row=5, column=2, pady=1)
- btnEquals=Button(calc, text="=", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="powder blue", command=added_value.sum_of_total).grid(row=5, column=3, pady=1)
- #===================Scientific Calculator====================================================================================================================================================
- btnPi=Button(calc, text='π', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.pi).grid(row=1, column=4, pady=1)
- btnCos=Button(calc, text="cos", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.cos).grid(row=1, column=5, pady=1)
- btnTan=Button(calc, text="tan", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.tan).grid(row=1, column=6, pady=1)
- btnSin=Button(calc, text="sin", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.sin).grid(row=1, column=7, pady=1)
- btn2Pi=Button(calc, text='2π', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.tau).grid(row=2, column=4, pady=1)
- btnCosh=Button(calc, text="cosh", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.cosh).grid(row=2, column=5, pady=1)
- btnTanh=Button(calc, text="tanh", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.tanh).grid(row=2, column=6, pady=1)
- btnSinh=Button(calc, text="sinh", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.sinh).grid(row=2, column=7, pady=1)
- btnLog=Button(calc, text='log', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.log).grid(row=3, column=4, pady=1)
- btninv=Button(calc, text="Inv", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=lambda:added_value.operation("inv")).grid(row=3, column=5, pady=1)
- btnMod=Button(calc, text="Mod", width=6, height=2, font=('arial', 20, 'bold'), bd=4, command=lambda:added_value.operation("mod")).grid(row=3, column=6, pady=1)
- btnE=Button(calc, text="e", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.e).grid(row=3, column=7, pady=1)
- btnLog2=Button(calc, text='log2', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.log2).grid(row=4, column=4, pady=1)
- btnDeg=Button(calc, text="deg", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.degrees).grid(row=4, column=5, pady=1)
- btnAcosh=Button(calc, text="acosh", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.acosh).grid(row=4, column=6, pady=1)
- btnAsinh=Button(calc, text="asinh", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="gray", command=added_value.asinh).grid(row=4, column=7, pady=1)
- btnLog10=Button(calc, text='log10', width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.log10).grid(row=5, column=4, pady=1)
- btnLog1p=Button(calc, text="log1p", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.log1p).grid(row=5, column=5, pady=1)
- btnExpm1=Button(calc, text="expm1", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.expm1).grid(row=5, column=6, pady=1)
- btnLgamma=Button(calc, text="lgamma", width=6, height=2, font=('arial', 20, 'bold'), bd=4, bg="Aqua", command=added_value.lgamma).grid(row=5, column=7, pady=1)
- #===============================Display Text======================================================================================================================================
- lblDisplay=Label(calc, text="Scientific Calculator", font=('arial', 30, 'bold'), justify =CENTER)
- lblDisplay.grid(row=0, column=4, columnspan=4)
- lblDisplay=Label(calc, text="Aarsh Raghuvanshi", font=('arial', 30, 'bold'), justify =CENTER)
- lblDisplay.grid(row=6, column=0, columnspan=4)
- #=======================Menu and function===========================================================
- def iExit():
- iExit = tkinter.messagebox.askyesno("Scientific Calculator", "Do you really want to exit?")
- if iExit>0:
- root.destroy()
- return
- def Scientific():
- root.resizable(width=False, height=False)
- root.geometry("944x624+20+20")
- def Standard():
- root.resizable(width=False, height=False)
- root.geometry("480x624+20+20")
- def term_of_use():
- tmsg.showinfo('Terms of Use ','IF YOU LIVE IN (OR IF YOUR PRINCIPAL PLACE OF BUSINESS IS IN) INDIA, PLEASE READ THE BINDING ARBITRATION CLAUSE AND CLASS ACTION WAIVER IN SECTION 11. IT AFFECTS HOW DISPUTES ARE RESOLVED.')
- def send_feedback():
- ans=tmsg.askquestion('Feedback Hub','Was your experience good with us ? ')
- if ans=='yes':
- tmsg.showinfo('Feedback','Please Rate us on Microsoft Store')
- else:
- tmsg.showinfo('Feedback','We will contact you soon to know about your bad experience')
- def paste():
- txtDisplay.event_generate(('<<Paste>>'))
- def copy():
- txtDisplay.event_generate(('<<Copy>>'))
- def cut():
- txtDisplay.event_generate(('<<Cut>>'))
- def print_display():
- txtDisplay.event_generate(('<<Print>>'))
- def view_help():
- tmsg.showinfo('help'," We will contact you soon")
- def clearall(self):
- self.string.set("")
- def about_calculator():
- tmsg.showinfo('About Notepad','This Calculator is created by Aarsh Raghuvanshi')
- def Open():
- global file
- file=askopenfilename(defaultextension='.txt',filetypes=[('All Files','*.*'),('Text documents','*.txt')])
- if file=='':
- file=None
- else:
- root.title(os.path.basename(file+" - Notepad"))
- Textarea.delete(1.0, END)
- f=open(file, 'r')
- Textarea.insert(1.0, f.read())
- f.close()
- menubar = Menu(calc)
- filemenu = Menu(menubar, tearoff=0)
- menubar.add_cascade(label = "File", menu=filemenu)
- filemenu.add_command(label = "Standadrd", command = Standard)
- filemenu.add_command(label = "Scientific", command = Scientific)
- filemenu.add_separator()
- filemenu.add_command(label = "Print", command = print_display)
- filemenu.add_separator()
- filemenu.add_command(label = "Open", command = Open)
- filemenu.add_separator()
- filemenu.add_command(label = "Exit", command = iExit)
- editmenu = Menu(menubar, tearoff =0)
- menubar.add_cascade(label = "Edit", menu=editmenu)
- editmenu.add_command(label = "Cut", command=cut)
- editmenu.add_command(label = "Copy", command=copy)
- editmenu.add_separator()
- editmenu.add_command(label = "Paste", command=paste)
- aboutmenu = Menu(menubar, tearoff =0)
- menubar.add_cascade(label = "About", menu=aboutmenu)
- aboutmenu.add_command(label="Term of Use",command = term_of_use)
- aboutmenu.add_command(label="Send Feedback",command = send_feedback)
- helpmenu = Menu(menubar, tearoff =0)
- menubar.add_cascade(label = "Help", menu=helpmenu)
- helpmenu.add_command(label = "View help", command=view_help)
- #====================Main Loop=============================================================================
- root.config(menu=menubar)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement