Advertisement
aricleather

Basic_Tkinter_Calculator.py

Sep 18th, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. from tkinter import *
  2. import math
  3.  
  4. while True:
  5.    
  6.     ans = 0
  7.  
  8.     root = Tk()
  9.  
  10.     root.wm_attributes('-topmost', 1)
  11.     root.wm_title("Calculator")
  12.  
  13.     num_entry1 = IntVar()
  14.     num_entry2 = IntVar()
  15.  
  16.     entry1 = Entry(root, textvariable=num_entry1, width=53)
  17.     entry1.pack()
  18.     entry1.delete(0, END)
  19.     entry1.insert(0, "0")
  20.  
  21.     entry2 = Entry(root, textvariable=num_entry2, width=53)
  22.     entry2.pack()
  23.     entry2.delete(0, END)
  24.     entry2.insert(0, "0")
  25.  
  26.     answer = Text(root, width=40, height=2)
  27.     answer.pack()
  28.     answer.insert("1.0", ans)
  29.     answer.config(state=DISABLED)
  30.  
  31.     operation = StringVar(root)
  32.     operation.set("Add")
  33.     option = OptionMenu(root, operation, "Add", "Subtract", "Multiply", "Divide (Float)", "X^Y",
  34.                         "√num1", "√num2", "num1!", "num2!", "Pythagoras")
  35.     option.pack()
  36.  
  37.     def calc():
  38.  
  39.         global ans
  40.  
  41.         num1 = num_entry1.get()
  42.         num2 = num_entry2.get()
  43.         op = operation.get()
  44.  
  45.         if op == 'Add':
  46.             ans = num1 + num2
  47.         elif op == 'Subtract':
  48.             ans = num1 - num2
  49.         elif op == 'Multiply':
  50.             ans = num1 * num2
  51.         elif op == 'Divide (Float)':
  52.             ans = num1 / num2
  53.         elif op == 'X^Y':
  54.             ans = num1 ** num2
  55.         elif op == '√num1':
  56.             ans = math.sqrt(num1)
  57.         elif op == '√num2':
  58.             ans = math.sqrt(num2)
  59.         elif op == 'num1!':
  60.             ans = math.factorial(num1)
  61.         elif op == 'num2!':
  62.             ans = math.factorial(num2)
  63.         elif op == 'Pythagoras':
  64.             ans = math.sqrt(((num1 ** 2) + (num2 ** 2)))
  65.         else:
  66.             pass
  67.  
  68.         answer.config(state=NORMAL)
  69.         answer.delete("1.0", END)
  70.         answer.insert("1.0", ans)
  71.         answer.config(state=DISABLED)
  72.  
  73.         return
  74.  
  75.     def calcquit():
  76.         global z
  77.         z = 1
  78.         root.destroy()
  79.  
  80.     button1 = Button(root, text="Calculate", command=calc)
  81.     button1.pack()
  82.  
  83.     button2 = Button(root, text="Quit", command=calcquit)
  84.     button2.pack()
  85.  
  86.     mainloop()
  87.  
  88.     if z == 1:
  89.         break
  90.     else:
  91.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement