Advertisement
de4fening

Untitled

Dec 3rd, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.32 KB | None | 0 0
  1. from tkinter import *
  2. import math
  3. import tkinter.messagebox
  4. import pyperclip
  5. import webbrowser
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. root = Tk()
  10. root.title("Scientific calculator")
  11. root.configure(background="powder blue")
  12. root.resizable(width=False, height=False)
  13. root.geometry("900x850+0+0")
  14.  
  15. calc = Frame(root)
  16. calc.grid()
  17.  
  18. calc_entry = Entry(root, width=33)
  19. calc_entry.grid()
  20.  
  21.  
  22. class Calc():
  23.  
  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.  
  32.     def numberEnter(self, num):
  33.         self.result = False
  34.         firstnum = txtDisplay.get()
  35.         secondnum = str(num)
  36.         if self.input_value:
  37.             self.current = secondnum
  38.             self.input_value = False
  39.         else:
  40.             if secondnum == ".":
  41.                 if secondnum in firstnum:
  42.                     return
  43.             self.current = firstnum + secondnum
  44.         self.display(self.current)
  45.  
  46.     def sum_of_total(self):
  47.         self.result = False
  48.         self.current = float(self.current)
  49.         if self.check_sum == True:
  50.             self.valid_function()
  51.         else:
  52.             self.total = float(txtDisplay.get())
  53.  
  54.     def display(self, value):
  55.         txtDisplay.delete(0, END)
  56.         txtDisplay.insert(0, value)
  57.  
  58.     def valid_function(self):
  59.         if self.op == "add":
  60.             self.total += self.current
  61.         if self.op == "sub":
  62.             self.total -= self.current
  63.         if self.op == "mult":
  64.             self.total *= self.current
  65.         if self.op == "divide":
  66.             self.total /= self.current
  67.         if self.op == "mod":
  68.             self.total %= self.current
  69.         self.input_value = True
  70.         self.check_sum = False
  71.         self.display(self.total)
  72.  
  73.     def operation(self, op):
  74.         self.current = float(self.current)
  75.         if self.check_sum:
  76.             self.valid_function()
  77.         elif not self.result:
  78.             self.total = self.current
  79.             self.input_value = True
  80.         self.check_sum = True
  81.         self.op = op
  82.         self.result = False
  83.  
  84.     def Clear_Entry(self):
  85.         self.result = False
  86.         self.current = "0"
  87.         self.display(0)
  88.         self.input_value = True
  89.  
  90.     def All_Clear_Entry(self):
  91.         self.Clear_Entry()
  92.         self.total = 0
  93.  
  94.     def mathsPM(self):
  95.         self.result = False
  96.         self.current = -(float(txtDisplay.get()))
  97.         self.display(self.current)
  98.  
  99.     def pi(self):
  100.         self.result = False
  101.         self.current = math.pi
  102.         self.display(self.current)
  103.  
  104.     def tau(self):
  105.         self.result = False
  106.         self.current = math.tau
  107.         self.display(self.current)
  108.  
  109.     def e(self):
  110.         self.result = False
  111.         self.current = math.e
  112.         self.display(self.current)
  113.  
  114.     def squared(self):
  115.         self.result = False
  116.         self.current = math.sqrt(float(txtDisplay.get()))
  117.         self.display(self.current)
  118.  
  119.     def cos(self):
  120.         self.result = False
  121.         self.current = math.cos(math.radians(float(txtDisplay.get())))
  122.         self.display(self.current)
  123.  
  124.     def cosh(self):
  125.         self.result = False
  126.         self.current = math.cosh(math.radians(float(txtDisplay.get())))
  127.         self.display(self.current)
  128.  
  129.     def tan(self):
  130.         self.result = False
  131.         self.current = math.tan(math.radians(float(txtDisplay.get())))
  132.         self.display(self.current)
  133.  
  134.     def tanh(self):
  135.         self.result = False
  136.         self.current = math.tanh(math.radians(float(txtDisplay.get())))
  137.         self.display(self.current)
  138.  
  139.     def sin(self):
  140.         self.result = False
  141.         self.current = math.sin(math.radians(float(txtDisplay.get())))
  142.         self.display(self.current)
  143.  
  144.     def sinh(self):
  145.         self.result = False
  146.         self.current = math.sinh(math.radians(float(txtDisplay.get())))
  147.         self.display(self.current)
  148.  
  149.     def log(self):
  150.         self.result = False
  151.         self.current = math.log(float(txtDisplay.get()))
  152.         self.display(self.current)
  153.  
  154.     def log10(self):
  155.         self.result = False
  156.         self.current = math.log10(float(txtDisplay.get()))
  157.         self.display(self.current)
  158.  
  159.     def exp(self):
  160.         self.result = False
  161.         self.current = math.exp(float(txtDisplay.get()))
  162.         self.display(self.current)
  163.  
  164.     def expm1(self):
  165.         self.result = False
  166.         self.current = math.expm1(float(txtDisplay.get()))
  167.         self.display(self.current)
  168.  
  169.     def lgamma(self):
  170.         self.result = False
  171.         self.current = math.lgamma(float(txtDisplay.get()))
  172.         self.display(self.current)
  173.  
  174.     def degrees(self):
  175.         self.result = False
  176.         self.current = math.degrees(float(txtDisplay.get()))
  177.         self.display(self.current)
  178.  
  179.     def log2(self):
  180.         self.result = False
  181.         self.current = math.log2(float(txtDisplay.get()))
  182.         self.display(self.current)
  183.  
  184.     def log1p(self):
  185.         self.result = False
  186.         self.current = math.log1p(float(txtDisplay.get()))
  187.         self.display(self.current)
  188.  
  189.  
  190. class GraphMode():
  191.     def __init__(self):
  192.         self.current = ""
  193.         self.input_value = True
  194.         self.check_sum = False
  195.         self.op = ""
  196.         self.result = False
  197.  
  198.     def check_entering(self):
  199.         numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  200.         if self.current not in numbers:
  201.             self.input_value=False
  202.  
  203.  
  204.  
  205. added_value = Calc()
  206.  
  207. txtDisplay = Entry(calc, font=("arial", 20, "bold"), bg="powder blue", bd=30, width=28, justify=RIGHT)
  208. txtDisplay.grid(row=0, column=0, columnspan=4, pady=1)
  209. txtDisplay.insert(0, "0")
  210.  
  211. numberpad = "789456123"
  212. i = 0
  213. btn = []
  214. for j in range(2, 5):
  215.     for k in range(3):
  216.         btn.append(Button(calc, width=6, height=2, font=("arial", 20, "bold"), bd=4, text=numberpad[i]))
  217.         btn[i].grid(row=j, column=k, pady=1)
  218.         btn[i]["command"] = lambda x=numberpad[i]: added_value.numberEnter(x)
  219.         i += 1
  220.  
  221. # ======================================================================================================================
  222. btnClear = Button(calc, text=chr(67), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  223.                   bg="powder blue", command=added_value.Clear_Entry).grid(row=1, column=0, pady=1)
  224. btnAllClear = Button(calc, text=chr(67) + chr(69), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  225.                      bg="powder blue", command=added_value.All_Clear_Entry).grid(row=1, column=1, pady=1)
  226. btnSq = Button(calc, text="√", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  227.                bg="powder blue", command=added_value.squared).grid(row=1, column=2, pady=1)
  228. btnAdd = Button(calc, text="+", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  229.                 bg="powder blue",
  230.                 command=lambda: added_value.operation("add")).grid(row=1, column=3, pady=1)
  231.  
  232. btnSub = Button(calc, text="-", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  233.                 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,
  235.                  bg="powder blue", command=lambda: added_value.operation("mult")
  236.                  ).grid(row=3, column=3, pady=1)
  237. btnDiv = Button(calc, text=chr(247), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  238.                 bg="powder blue", command=lambda: added_value.operation("divide")
  239.                 ).grid(row=4, column=3, pady=1)
  240.  
  241. btnZero = Button(calc, text="0", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  242.                  bg="powder blue", command=lambda: added_value.numberEnter(0)
  243.                  ).grid(row=5, column=0, pady=1)
  244.  
  245. btnDot = Button(calc, text=".", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  246.                 bg="powder blue", command=lambda: added_value.numberEnter(".")
  247.                 ).grid(row=5, column=1, pady=1)
  248. btnPM = Button(calc, text=chr(177), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  249.                bg="powder blue", command=added_value.mathsPM).grid(row=5, column=2, pady=1)
  250. btnEquals = Button(calc, text="=", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  251.                    bg="powder blue", command=added_value.sum_of_total).grid(row=5, column=3, pady=1)
  252. # ==================================================Scientific Calculator===============================================
  253. btnPi = Button(calc, text=chr(960), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  254.                bg="powder blue", command=added_value.pi).grid(row=1, column=4, pady=1)
  255. btnCos = Button(calc, text="cos", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  256.                 bg="powder blue", command=added_value.cos).grid(row=1, column=5, pady=1)
  257. btnSin = Button(calc, text="sin", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  258.                 bg="powder blue", command=added_value.sin).grid(row=1, column=6, pady=1)
  259. btnTan = Button(calc, text="tan", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  260.                 bg="powder blue", command=added_value.tan).grid(row=1, column=7, pady=1)
  261. # ======================================================================================================================
  262. btn2Pi = Button(calc, text=("2", chr(960)), width=6, height=2, font=("arial", 20, "bold"), bd=4,
  263.                 bg="powder blue", command=added_value.tau).grid(row=2, column=4, pady=1)
  264. btnCosh = Button(calc, text="cosh", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.cosh
  265.                  ).grid(row=2, column=5, pady=1)
  266. btnSinh = Button(calc, text="sinh", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.sinh
  267.                  ).grid(row=2, column=6, pady=1)
  268.  
  269. btnTanh = Button(calc, text="tanh", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.tanh
  270.                  ).grid(row=2, column=7, pady=1)
  271. # ======================================================================================================================
  272.  
  273. btnLog = Button(calc, text="log", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  274.                 bg="powder blue", command=added_value.log).grid(row=3, column=4, pady=1)
  275. btnExp = Button(calc, text="exp", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.exp
  276.                 ).grid(row=3, column=5, pady=1)
  277. btnMod = Button(calc, text="mod", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  278.                 command=lambda: added_value.operation("mod")).grid(row=3, column=6, pady=1)
  279. btnE = Button(calc, text="e", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.e
  280.               ).grid(row=3, column=7, pady=1)
  281. # ======================================================================================================================
  282. btnLog2 = Button(calc, text="log2", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  283.                  bg="powder blue", command=added_value.log2).grid(row=4, column=4, pady=1)
  284. btnDeg = Button(calc, text="deg", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.degrees
  285.                 ).grid(row=4, column=5, pady=1)
  286. btnaCosh = Button(calc, text="acosh", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.cosh
  287.                   ).grid(row=4, column=6, pady=1)
  288. btnaSinh = Button(calc, text="asinh", width=6, height=2, font=("arial", 20, "bold"), bd=4, command=added_value.sinh
  289.                   ).grid(row=4, column=7, pady=1)
  290. # ======================================================================================================================
  291. btnLog10 = Button(calc, text="log10", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  292.                   bg="powder blue", command=added_value.log10).grid(row=5, column=4, pady=1)
  293. btnLog1p = Button(calc, text="log1p", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  294.                   bg="powder blue", command=added_value.log1p).grid(row=5, column=5, pady=1)
  295. btnExpm1 = Button(calc, text="expm1", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  296.                   bg="powder blue", command=added_value.expm1).grid(row=5, column=6, pady=1)
  297. btnLgamma = Button(calc, text="lgamma", width=6, height=2, font=("arial", 20, "bold"), bd=4,
  298.                    bg="powder blue", command=added_value.lgamma).grid(row=5, column=7, pady=1)
  299.  
  300. lblDisplay = Label(calc, text="Scientific calculator", font=("arial", 30, "bold"), justify=CENTER)
  301. lblDisplay.grid(row=0, column=4, columnspan=4)
  302.  
  303.  
  304. # =================================================Menu and function====================================================
  305.  
  306. def gMode():
  307.     graph_root = Tk()
  308.     graph_root.title("Graph Mode")
  309.     graph_root.configure(background="powder blue")
  310.     graph_root.resizable(width=False, height=False)
  311.     graph_root.geometry("900x1000+0+0")
  312.  
  313.     graph_calc = Frame(graph_root)
  314.     graph_calc.grid()
  315.  
  316.     graph_calc_entry = Entry(graph_root, width=33)
  317.     graph_calc_entry.grid()
  318.  
  319.     G_txtDisplay_start = Entry(graph_calc, font=("arial", 20, "bold"), bg="powder blue", bd=30, width=28, justify=RIGHT)
  320.     G_txtDisplay_start.grid(row=0, column=0, columnspan=4, pady=1)
  321.     G_txtDisplay_start.insert(0, "0")
  322.  
  323.     G_txtDisplay_count = Entry(graph_calc, font=("arial", 20, "bold"), bg="powder blue", bd=30, width=28, justify=RIGHT)
  324.     G_txtDisplay_count.grid(row=1, column=0, columnspan=4, pady=1)
  325.     G_txtDisplay_count.insert(0, "0")
  326.  
  327.     G_txtDisplay_math_formula = Entry(graph_calc, font=("arial", 20, "bold"), bg="powder blue", bd=30, width=28,
  328.                                       justify=RIGHT)
  329.     G_txtDisplay_math_formula.grid(row=2, column=0, columnspan=4, pady=1)
  330.     G_txtDisplay_math_formula.insert(0, "0")
  331.  
  332.  
  333.  
  334. def iExit():
  335.     iExit = tkinter.messagebox.askyesno("Scientific calculator", "Confirm if you want to exit")
  336.     if iExit > 0:
  337.         root.destroy()
  338.         return
  339.  
  340.  
  341. def Scientific():
  342.     root.resizable(width=False, height=False)
  343.     root.geometry("1760x850+0+0")  # 1750x850+0+0
  344.  
  345.  
  346. def Standard():
  347.     root.resizable(width=False, height=False)
  348.     root.geometry("900x850+0+0")
  349.  
  350.  
  351. def Copy():
  352.     pyperclip.copy(txtDisplay.get())
  353.  
  354.  
  355. def Paste():
  356.     if txtDisplay.get() == "0":
  357.         txtDisplay.delete(0, END)
  358.         txtDisplay.insert(0, str(pyperclip.paste()))
  359.     else:
  360.         txtDisplay.insert(0, str(pyperclip.paste()))
  361.  
  362.  
  363. def Cut():
  364.     pyperclip.copy(txtDisplay.get())
  365.     txtDisplay.delete(0, END)
  366.     txtDisplay.insert(0, "0")
  367.  
  368.  
  369. def Help():
  370.     webbrowser.open(r"Help.pdf")
  371.  
  372.  
  373. menubar = Menu(calc)
  374.  
  375. filemenu = Menu(menubar, tearoff=0)
  376. menubar.add_cascade(label="File", menu=filemenu)
  377. filemenu.add_command(label="Standard", command=Standard)
  378. filemenu.add_command(label="Scientific", command=Scientific)
  379. #filemenu.add_command(label="Graph Mode", command=gMode)
  380. filemenu.add_separator()
  381. filemenu.add_command(label="Exit", command=iExit)
  382.  
  383. editmenu = Menu(menubar, tearoff=0)
  384. menubar.add_cascade(label="Edit", menu=editmenu)
  385. editmenu.add_command(label="Cut", command=Cut)
  386. editmenu.add_command(label="Copy", command=Copy)
  387. editmenu.add_separator()
  388. editmenu.add_command(label="Paste", command=Paste)
  389.  
  390. helpmenu = Menu(menubar, tearoff=0)
  391. menubar.add_cascade(label="Help", menu=helpmenu)
  392. helpmenu.add_command(label="View Help ", command=Help)
  393.  
  394. root.config(menu=menubar)
  395. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement