Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter
- # Contains the basic calculator functions
- class Calculator():
- def __init__(self):
- self.total = 0
- self.current = ""
- self.new_num = True
- self.op_pending = False
- self.op = ""
- self.eq = False
- def num_press(self, num):
- self.eq = False
- temp = text_box.get()
- temp2 = str(num)
- if self.new_num:
- self.current = temp2
- self.new_num = False
- else:
- if temp2 == '.':
- if temp2 in temp:
- return
- self.current = temp + temp2
- self.display(self.current)
- def calc_total(self):
- self.eq = True
- self.current = float(self.current)
- if self.op_pending == True:
- self.do_sum()
- else:
- self.total = float(text_box.get())
- def display(self, value):
- text_box.delete(0, tkinter.END)
- text_box.insert(0, value)
- def do_sum(self):
- if self.op == "add":
- self.total += self.current
- if self.op == "minus":
- self.total -= self.current
- if self.op == "times":
- self.total *= self.current
- if self.op == "divide":
- self.total /= self.current
- self.new_num = True
- self.op_pending = False
- self.display(self.total)
- def operation(self, op):
- self.current = float(self.current)
- if self.op_pending:
- self.do_sum()
- elif not self.eq:
- self.total = self.current
- self.new_num = True
- self.op_pending = True
- self.op = op
- self.eq = False
- def cancel(self):
- self.eq = False
- self.current = "0"
- self.display(0)
- self.new_num = True
- def all_cancel(self):
- self.cancel()
- self.total = 0
- def sign(self):
- self.eq = False
- self.current = -(float(text_box.get()))
- self.display(self.current)
- sum1 = Calculator()
- root = tkinter.Tk()
- calculator = tkinter.Frame(root)
- calculator.grid()
- root.resizable(width=tkinter.FALSE, height=tkinter.FALSE) # Ensures that the space cannot be resized
- root.title("Calculator")
- text_box = tkinter.Entry(calculator, justify=tkinter.RIGHT)
- text_box.grid(row=0, column=0, columnspan=3, pady=5)
- text_box.insert(0, "0")
- numbers = "789456123" # creates the calculator buttons
- i = 0
- button = []
- for j in range(1, 4):
- for k in range(3):
- button.append(tkinter.Button(calculator, text=numbers[i]))
- button[i].grid(row=j, column=k, pady=5)
- button[i]["command"] = lambda x=numbers[i]: sum1.num_press(x)
- i += 1
- b0 = tkinter.Button(calculator, text="0")
- b0["command"] = lambda: sum1.num_press(0)
- b0.grid(row=4, column=1, pady=5)
- b_divide = tkinter.Button(calculator, text="/")
- b_divide["command"] = lambda: sum1.operation("divide")
- b_divide.grid(row=1, column=3, pady=5)
- b_multiply = tkinter.Button(calculator, text="x")
- b_multiply["command"] = lambda: sum1.operation("times")
- b_multiply.grid(row=2, column=3, pady=5)
- b_minus = tkinter.Button(calculator, text="-")
- b_minus["command"] = lambda: sum1.operation("minus")
- b_minus.grid(row=3, column=3, pady=5)
- decimal = tkinter.Button(calculator, text=".")
- decimal["command"] = lambda: sum1.num_press(".")
- decimal.grid(row=4, column=0, pady=5)
- b_add = tkinter.Button(calculator, text="+")
- b_add["command"] = lambda: sum1.operation("add")
- b_add.grid(row=4, column=3, pady=5)
- b_sign = tkinter.Button(calculator, text="+/-")
- b_sign["command"] = sum1.sign
- b_sign.grid(row=5, column=0, pady=5)
- b_clear = tkinter.Button(calculator, text="C", bg="yellow")
- b_clear["command"] = sum1.cancel
- b_clear.grid(row=5, column=1, pady=5)
- all_clear = tkinter.Button(calculator, text="AC", bg="red")
- all_clear["command"] = sum1.all_cancel
- all_clear.grid(row=5, column=2, pady=5)
- b_equal = tkinter.Button(calculator, text="=", bg="green")
- b_equal["command"] = sum1.calc_total
- b_equal.grid(row=5, column=3, pady=5)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement