Advertisement
Ar_3945

Calc

Nov 15th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.29 KB | None | 0 0
  1. from tkinter import*
  2. import math
  3. import parser
  4. import tkinter.messagebox
  5. import tkinter.messagebox as tmsg
  6. import os
  7. import time
  8. from math import sin,cos,tan,log
  9. from tkinter.filedialog import askopenfilename,askopenfile,asksaveasfile,asksaveasfilename
  10.  
  11. root = Tk()
  12. root.title("Scientific Calculator")
  13. root.configure(background="Powder blue")
  14. root.resizable(width=False, height=False)
  15. root.geometry("480x624+20+20")
  16.  
  17. calc = Frame(root)
  18. calc.grid()
  19.  
  20.  
  21. #=============Functions==================================================================================================
  22.  
  23. class Calc():
  24.     def __init__(self):
  25.         self.total=0
  26.         self.current=""
  27.         self.input_value=True
  28.         self.check_sum=False
  29.         self.op=""
  30.         self.result=False
  31.     def numberEnter(self, num):
  32.         self.result=False
  33.         firstnum=txtDisplay.get()
  34.         secondnum=str(num)
  35.         if self.input_value:
  36.             self.current=secondnum
  37.             self.input_value=False
  38.         else:
  39.             if secondnum=='.':
  40.                 if secondnum in firstnum:
  41.                     return
  42.             self.current=firstnum+secondnum
  43.         self.display(self.current)
  44.  
  45.     def sum_of_total(self):
  46.         self.result=True
  47.         self.current=float(self.current)
  48.         if self.check_sum==True:
  49.             self.valid_function()
  50.         else:
  51.             self.total=float(txtDisplay.get())
  52.  
  53.     def valid_function(self):
  54.         if self.op=="add":
  55.             self.total+=self.current
  56.         if self.op=="sub":
  57.             self.total-=self.current
  58.         if self.op=="multi":
  59.             self.total*=self.current
  60.         if self.op=="divide":
  61.             self.total/=self.current    
  62.         if self.op=="mod":
  63.             self.total%=self.current
  64.         if self.op=="inv":
  65.             self.total=1/self.current
  66.         self.input_value=True
  67.         self.check_sum=False
  68.         self.display(self.total)
  69.        
  70.     def operation(self, op):
  71.         self.current=float(self.current)
  72.         if self.check_sum:
  73.             self.valid_function()
  74.         elif not self.result:
  75.             self.total=self.current
  76.             self.input_value=True
  77.         self.check_sum=True
  78.         self.op=op
  79.         self.result=False
  80.  
  81.     def Clear_Entry(self):
  82.         self.result=False
  83.         self.current="0"
  84.         self.display(0)
  85.         self.input_value=True
  86.  
  87.     def all_Clear_Entry(self):
  88.         self.Clear_Entry()
  89.         self.total=0
  90.  
  91.     def tanh(self):
  92.         self.reult=False
  93.         self.current=math.tanh(math.radians(float(txtDisplay.get())))
  94.         self.display(self.current)
  95.  
  96.     def tan(self):
  97.         self.reult=False
  98.         self.current=math.tan(math.radians(float(txtDisplay.get())))
  99.         self.display(self.current)
  100.        
  101.     def sinh(self):
  102.         self.reult=False
  103.         self.current=math.sinh(math.radians(float(txtDisplay.get())))
  104.         self.display(self.current)
  105.        
  106.     def sin(self):
  107.         self.reult=False
  108.         self.current=math.sin(math.radians(float(txtDisplay.get())))
  109.         self.display(self.current)
  110.        
  111.     def log(self):
  112.         self.reult=False
  113.         self.current=math.log(float(txtDisplay.get()))
  114.         self.display(self.current)
  115.        
  116.     def exp(self):
  117.         self.reult=False
  118.         self.current=math.exp(float(txtDisplay.get()))
  119.         self.display(self.current)
  120.        
  121.     def mathsPM(self):
  122.         self.reult=False
  123.         self.current=-(float(txtDisplay.get()))
  124.         self.display(self.current)
  125.  
  126.     def squared(self):
  127.         self.reult=False
  128.         self.current=math.sqrt(float(txtDisplay.get()))
  129.         self.display(self.current)
  130.        
  131.     def cos(self):
  132.         self.reult=False
  133.         self.current=math.cos(math.radians(float(txtDisplay.get())))
  134.         self.display(self.current)
  135.        
  136.     def cosh(self):
  137.         self.reult=False
  138.         self.current=math.cosh(math.radians(float(txtDisplay.get())))
  139.         self.display(self.current)
  140.  
  141.  
  142.     def display(self, value):
  143.         txtDisplay.delete(0, END)
  144.         txtDisplay.insert(0, value)
  145.  
  146.     def pi(self):
  147.         self.reult=False
  148.         self.current=math.pi
  149.         self.display(self.current)
  150.  
  151.  
  152.     def tau(self):
  153.         self.reult=False
  154.         self.current=math.tau
  155.         self.display(self.current)
  156.        
  157.     def e(self):
  158.         self.reult=False
  159.         self.current=math.e
  160.         self.display(self.current)
  161.  
  162.     def acosh(self):
  163.         self.result=False
  164.         self.current=math.acosh(float(txtDisplay.get()))
  165.         self.display(self.current)
  166.  
  167.     def asinh(self):
  168.         self.result=False
  169.         self.current=math.asinh(float(txtDisplay.get()))
  170.         self.display(self.current)
  171.  
  172.     def expm1(self):
  173.         self.result=False
  174.         self.current=math.expm1(float(txtDisplay.get()))
  175.         self.display(self.current)
  176.  
  177.     def lgamma(self):
  178.         self.result=False
  179.         self.current=math.lgamma(float(txtDisplay.get()))
  180.         self.display(self.current)
  181.  
  182.     def degrees(self):
  183.         self.result=False
  184.         self.current=math.degrees(float(txtDisplay.get()))
  185.         self.display(self.current)
  186.  
  187.     def log2(self):
  188.         self.result=False
  189.         self.current=math.log2(float(txtDisplay.get()))
  190.         self.display(self.current)
  191.  
  192.     def log10(self):
  193.         self.result=False
  194.         self.current=math.log10(float(txtDisplay.get()))
  195.         self.display(self.current)
  196.  
  197.     def log1p(self):
  198.         self.result=False
  199.         self.current=math.log1p(float(txtDisplay.get()))
  200.         self.display(self.current)
  201.  
  202.    
  203.  
  204. added_value=Calc()
  205.  
  206. #================Display================================================================================================
  207.  
  208.  
  209. txtDisplay = Entry(calc, relief=SUNKEN, font=('arial', 20, 'bold'), bg="powder blue", bd=30, width=28, justify=RIGHT)
  210. txtDisplay.grid(row=0, column=0, columnspan=4, pady=1)
  211. txtDisplay.insert(0, "0")
  212.  
  213. #==================Numbers==============================================================================================
  214.  
  215. numberpad = "789456123"
  216. i=0
  217. btn=[]
  218. for j in range(2,5):
  219.     for k in range(3):
  220.         btn.append(Button(calc, width=6, height=2, font=('arial', 20, 'bold'), bd=4, text=numberpad[i]))
  221.         btn[i].grid(row=j, column=k, pady=1)
  222.         btn[i]["command"]=lambda x=numberpad[i]: added_value.numberEnter(x)
  223.         i+=1
  224.  
  225. #========================Standard Function==============================================================================================================================================
  226.  
  227. 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)
  228. 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)
  229.  
  230. 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)
  231. 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)
  232.  
  233. 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)
  234. 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)
  235.  
  236. 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)
  237. 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)
  238.  
  239. 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)
  240. 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)
  241.  
  242. 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)
  243.  
  244. #===================Scientific Calculator====================================================================================================================================================
  245.  
  246. 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)
  247. 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)
  248.  
  249. 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)
  250. 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)
  251.  
  252. 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)
  253. 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)
  254.  
  255. 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)
  256. 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)
  257.  
  258. 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)
  259. 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)
  260.  
  261. 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)
  262. 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)
  263.  
  264. 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)
  265. 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)
  266.  
  267. 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)
  268. 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)
  269.  
  270. 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)
  271. 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)
  272.  
  273. 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)
  274. 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)
  275.  
  276. #===============================Display Text======================================================================================================================================
  277.  
  278. lblDisplay=Label(calc, text="Scientific Calculator", font=('arial', 30, 'bold'), justify =CENTER)
  279. lblDisplay.grid(row=0, column=4, columnspan=4)
  280.  
  281. lblDisplay=Label(calc, text="Aarsh Raghuvanshi", font=('arial', 30, 'bold'), justify =CENTER)
  282. lblDisplay.grid(row=6, column=0, columnspan=4)
  283.  
  284.  
  285.                    
  286. #=======================Menu and function===========================================================
  287.  
  288. def iExit():
  289.     iExit = tkinter.messagebox.askyesno("Scientific Calculator", "Do you really want to exit?")
  290.     if iExit>0:
  291.         root.destroy()
  292.         return
  293.  
  294. def Scientific():
  295.     root.resizable(width=False, height=False)
  296.     root.geometry("944x624+20+20")
  297.  
  298. def Standard():
  299.     root.resizable(width=False, height=False)
  300.     root.geometry("480x624+20+20")
  301.  
  302. def term_of_use():
  303.     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.')
  304.  
  305. def send_feedback():
  306.     ans=tmsg.askquestion('Feedback Hub','Was your experience good with us ? ')
  307.     if ans=='yes':
  308.         tmsg.showinfo('Feedback','Please Rate us on Microsoft Store')
  309.     else:
  310.         tmsg.showinfo('Feedback','We will contact you soon to know about your bad experience')
  311.  
  312. def paste():
  313.     txtDisplay.event_generate(('<<Paste>>'))
  314.  
  315. def copy():
  316.     txtDisplay.event_generate(('<<Copy>>'))
  317.  
  318. def cut():
  319.     txtDisplay.event_generate(('<<Cut>>'))
  320.  
  321. def print_display():
  322.     txtDisplay.event_generate(('<<Print>>'))
  323.  
  324. def view_help():
  325.     tmsg.showinfo('help'," We will contact you soon")
  326.  
  327. def clearall(self):
  328.     self.string.set("")
  329.  
  330. def about_calculator():
  331.     tmsg.showinfo('About Notepad','This Calculator is created by Aarsh Raghuvanshi')
  332.  
  333. def Open():
  334.     global file
  335.     file=askopenfilename(defaultextension='.txt',filetypes=[('All Files','*.*'),('Text documents','*.txt')])
  336.     if file=='':
  337.         file=None
  338.     else:
  339.         root.title(os.path.basename(file+" - Notepad"))
  340.         Textarea.delete(1.0, END)
  341.         f=open(file, 'r')
  342.         Textarea.insert(1.0, f.read())
  343.         f.close()
  344. menubar = Menu(calc)
  345.  
  346.  
  347. filemenu = Menu(menubar, tearoff=0)
  348. menubar.add_cascade(label = "File", menu=filemenu)
  349. filemenu.add_command(label = "Standadrd", command = Standard)
  350. filemenu.add_command(label = "Scientific", command = Scientific)
  351. filemenu.add_separator()
  352. filemenu.add_command(label = "Print", command = print_display)
  353. filemenu.add_separator()
  354. filemenu.add_command(label = "Open", command = Open)
  355. filemenu.add_separator()
  356. filemenu.add_command(label = "Exit", command = iExit)
  357.  
  358. editmenu = Menu(menubar, tearoff =0)
  359. menubar.add_cascade(label = "Edit", menu=editmenu)
  360. editmenu.add_command(label = "Cut", command=cut)
  361. editmenu.add_command(label = "Copy", command=copy)
  362. editmenu.add_separator()
  363. editmenu.add_command(label = "Paste", command=paste)
  364.  
  365. aboutmenu = Menu(menubar, tearoff =0)
  366. menubar.add_cascade(label = "About", menu=aboutmenu)
  367. aboutmenu.add_command(label="Term of Use",command = term_of_use)
  368. aboutmenu.add_command(label="Send Feedback",command = send_feedback)
  369.  
  370. helpmenu = Menu(menubar, tearoff =0)
  371. menubar.add_cascade(label = "Help", menu=helpmenu)
  372. helpmenu.add_command(label = "View help", command=view_help)
  373.  
  374.  
  375. #====================Main Loop=============================================================================
  376.  
  377. root.config(menu=menubar)
  378. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement