Advertisement
Buffet_Time

allison_asdfsafasdfdsadfsaff

Nov 28th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. import tkinter
  2.  
  3.  
  4. # Contains the basic calculator functions
  5. class Calculator():
  6.     def __init__(self):
  7.         self.total = 0
  8.         self.current = ""
  9.         self.new_num = True
  10.         self.op_pending = False
  11.         self.op = ""
  12.         self.eq = False
  13.  
  14.     def num_press(self, num):
  15.         self.eq = False
  16.         temp = text_box.get()
  17.         temp2 = str(num)
  18.         if self.new_num:
  19.             self.current = temp2
  20.             self.new_num = False
  21.         else:
  22.             if temp2 == '.':
  23.                 if temp2 in temp:
  24.                     return
  25.             self.current = temp + temp2
  26.         self.display(self.current)
  27.  
  28.     def calc_total(self):
  29.         self.eq = True
  30.         self.current = float(self.current)
  31.         if self.op_pending == True:
  32.             self.do_sum()
  33.         else:
  34.             self.total = float(text_box.get())
  35.  
  36.     def display(self, value):
  37.         text_box.delete(0, tkinter.END)
  38.         text_box.insert(0, value)
  39.  
  40.     def do_sum(self):
  41.         if self.op == "add":
  42.             self.total += self.current
  43.         if self.op == "minus":
  44.             self.total -= self.current
  45.         if self.op == "times":
  46.             self.total *= self.current
  47.         if self.op == "divide":
  48.             self.total /= self.current
  49.         self.new_num = True
  50.         self.op_pending = False
  51.         self.display(self.total)
  52.  
  53.     def operation(self, op):
  54.         self.current = float(self.current)
  55.         if self.op_pending:
  56.             self.do_sum()
  57.         elif not self.eq:
  58.             self.total = self.current
  59.         self.new_num = True
  60.         self.op_pending = True
  61.         self.op = op
  62.         self.eq = False
  63.  
  64.     def cancel(self):
  65.         self.eq = False
  66.         self.current = "0"
  67.         self.display(0)
  68.         self.new_num = True
  69.  
  70.     def all_cancel(self):
  71.         self.cancel()
  72.         self.total = 0
  73.  
  74.     def sign(self):
  75.         self.eq = False
  76.         self.current = -(float(text_box.get()))
  77.         self.display(self.current)
  78.  
  79.  
  80. sum1 = Calculator()
  81. root = tkinter.Tk()
  82. calculator = tkinter.Frame(root)
  83. calculator.grid()
  84. root.resizable(width=tkinter.FALSE, height=tkinter.FALSE)  # Ensures that the space cannot be resized
  85.  
  86. root.title("Calculator")
  87. text_box = tkinter.Entry(calculator, justify=tkinter.RIGHT)
  88. text_box.grid(row=0, column=0, columnspan=3, pady=5)
  89. text_box.insert(0, "0")
  90.  
  91. numbers = "789456123"  # creates the calculator buttons
  92. i = 0
  93. button = []
  94. for j in range(1, 4):
  95.     for k in range(3):
  96.         button.append(tkinter.Button(calculator, text=numbers[i]))
  97.         button[i].grid(row=j, column=k, pady=5)
  98.         button[i]["command"] = lambda x=numbers[i]: sum1.num_press(x)
  99.         i += 1
  100.  
  101. b0 = tkinter.Button(calculator, text="0")
  102. b0["command"] = lambda: sum1.num_press(0)
  103. b0.grid(row=4, column=1, pady=5)
  104.  
  105. b_divide = tkinter.Button(calculator, text="/")
  106. b_divide["command"] = lambda: sum1.operation("divide")
  107. b_divide.grid(row=1, column=3, pady=5)
  108.  
  109. b_multiply = tkinter.Button(calculator, text="x")
  110. b_multiply["command"] = lambda: sum1.operation("times")
  111. b_multiply.grid(row=2, column=3, pady=5)
  112.  
  113. b_minus = tkinter.Button(calculator, text="-")
  114. b_minus["command"] = lambda: sum1.operation("minus")
  115. b_minus.grid(row=3, column=3, pady=5)
  116.  
  117. decimal = tkinter.Button(calculator, text=".")
  118. decimal["command"] = lambda: sum1.num_press(".")
  119. decimal.grid(row=4, column=0, pady=5)
  120.  
  121. b_add = tkinter.Button(calculator, text="+")
  122. b_add["command"] = lambda: sum1.operation("add")
  123. b_add.grid(row=4, column=3, pady=5)
  124.  
  125. b_sign = tkinter.Button(calculator, text="+/-")
  126. b_sign["command"] = sum1.sign
  127. b_sign.grid(row=5, column=0, pady=5)
  128.  
  129. b_clear = tkinter.Button(calculator, text="C", bg="yellow")
  130. b_clear["command"] = sum1.cancel
  131. b_clear.grid(row=5, column=1, pady=5)
  132.  
  133. all_clear = tkinter.Button(calculator, text="AC", bg="red")
  134. all_clear["command"] = sum1.all_cancel
  135. all_clear.grid(row=5, column=2, pady=5)
  136.  
  137. b_equal = tkinter.Button(calculator, text="=", bg="green")
  138. b_equal["command"] = sum1.calc_total
  139. b_equal.grid(row=5, column=3, pady=5)
  140.  
  141. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement