medmond919

Untitled

Jun 11th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. from math import *
  2. from tkinter import *
  3. import math
  4.  
  5.  
  6. expression = ""
  7.  
  8.  
  9. def func(function):
  10.  
  11.     if function == "sin":
  12.         equation.set(math.sin(eval(equation.get())))
  13.  
  14.     elif function == "cos":
  15.         equation.set(math.cos(eval(equation.get())))
  16.  
  17.     elif function == "tan":
  18.         equation.set(math.tan(eval(equation.get())))
  19.  
  20.  
  21. def press(num):
  22.     global expression
  23.     if num == "exp":
  24.         num = math.e
  25.     expression = expression + str(num)
  26.     equation.set(expression)
  27.  
  28.  
  29. def equalpress():
  30.     global expression
  31.     total = str(eval(expression))
  32.     equation.set(total)
  33.     expression = ""
  34.  
  35.  
  36. def clear():
  37.     global expression
  38.     expression = ""
  39.     equation.set("")
  40.  
  41.  
  42. # driver code
  43. if __name__ == "__main__":
  44.     # create window
  45.     gui = Tk()
  46.     # set background color
  47.     gui.configure(background="blue")
  48.     # set title
  49.     gui.title("Scientific calculator")
  50.     equation = StringVar()
  51.     # text-entry box for the expression
  52.     expression_field = Entry(gui, textvariable=equation)
  53.     expression_field.grid(columnspan=5, ipadx=90)
  54.     equation.set("enter your expression")
  55.  
  56.     # buttons
  57.     button1 = Button(gui, text='1', fg='black', bg='grey',
  58.                      command=lambda: press(1), height=1, width=7)
  59.     button1.grid(row=2, column=0)
  60.     button2 = Button(gui, text='2', fg='black', bg='grey',
  61.                      command=lambda: press(2), height=1, width=7)
  62.     button2.grid(row=2, column=1)
  63.     button3 = Button(gui, text='3', fg='black', bg='grey',
  64.                      command=lambda: press(3), height=1, width=7)
  65.     button3.grid(row=2, column=2)
  66.     button4 = Button(gui, text='4', fg='black', bg='grey',
  67.                      command=lambda: press(4), height=1, width=7)
  68.     button4.grid(row=3, column=0)
  69.     button5 = Button(gui, text='5', fg='black', bg='grey',
  70.                      command=lambda: press(5), height=1, width=7)
  71.     button5.grid(row=3, column=1)
  72.     button6 = Button(gui, text='6', fg='black', bg='grey',
  73.                      command=lambda: press(6), height=1, width=7)
  74.     button6.grid(row=3, column=2)
  75.     button7 = Button(gui, text='7', fg='black', bg='grey',
  76.                      command=lambda: press(7), height=1, width=7)
  77.     button7.grid(row=4, column=0)
  78.     button8 = Button(gui, text='8', fg='black', bg='grey',
  79.                      command=lambda: press(8), height=1, width=7)
  80.     button8.grid(row=4, column=1)
  81.     button9 = Button(gui, text='9', fg='black', bg='grey',
  82.                      command=lambda: press(9), height=1, width=7)
  83.     button9.grid(row=4, column=2)
  84.     button0 = Button(gui, text='0', fg='black', bg='grey',
  85.                      command=lambda: press(0), height=1, width=7)
  86.     button0.grid(row=5, column=1)
  87.  
  88.     plus = Button(gui, text='+', fg='black', bg='grey',
  89.                   command=lambda: press('+'), height=1, width=7)
  90.     plus.grid(row=2, column=3)
  91.     minus = Button(gui, text='-', fg='black', bg='grey',
  92.                    command=lambda: press('-'), height=1, width=7)
  93.     minus.grid(row=3, column=3)
  94.     multiply = Button(gui, text='*', fg='black', bg='grey',
  95.                       command=lambda: press('*'), height=1, width=7)
  96.     multiply.grid(row=4, column=3)
  97.     divide = Button(gui, text='/', fg='black', bg='grey',
  98.                     command=lambda: press('/'), height=1, width=7)
  99.     divide.grid(row=5, column=3)
  100.     # scientific functions
  101.  
  102.     equal = Button(gui, text='=', fg='black', bg='grey',
  103.                    command=equalpress, height=1, width=7)
  104.     equal.grid(row=5, column=2)
  105.     clear = Button(gui, text='Clear', fg='black', bg='grey',
  106.                    command=clear, height=1, width=7)
  107.     clear.grid(row=5, column=0)
  108.  
  109.     exponent = Button(gui, text='e', fg='black', bg='grey',
  110.                       command=lambda: press('exp'), height=1, width=7)
  111.     exponent.grid(row=2, column=4)
  112.     sin = Button(gui, text='sin', fg='black', bg='grey',
  113.                  command=lambda: func('sin'), height=1, width=7)
  114.     sin.grid(row=3, column=4)
  115.  
  116.     cos = Button(gui, text='cos', fg='black', bg='grey',
  117.                  command=lambda: func('cos'), height=1, width=7)
  118.     cos.grid(row=4, column=4)
  119.  
  120.     tan = Button(gui, text='tan', fg='black', bg='grey',
  121.                  command=lambda: func('tan'), height=1, width=7)
  122.     tan.grid(row=5, column=4)
  123.  
  124.     # run GUI
  125.     gui.mainloop()
Add Comment
Please, Sign In to add comment