basically all i want to do is get my AC button working so that it clears the equation in the box, and sets it back to 0. I currently have a "C" button that clears the users last entry entered e.g 567 would become 56. I just need to add on an "AC" button that clears everything. Any ideas?
from tkinter import *
class Calc():
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:
retu
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, 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):
global var
self.eq = False
self.current = self.current[:-1] # Remove last digit
if self.current: # If there is at list one digit
var.set(self.current)
else: # In this case display 0 in text_box
var.set(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 = Calc()
root = Tk()
calc = Frame(root,bg="powder blue")
calc.grid()
root.title("Calculator")
#text_box = Entry(calc, font=("aerial",20,"bold"),justify=RIGHT, bd=45, bg="seashell3")
#text_box.grid(row = 0, column = 0, columnspan = 6,pady=30, padx=100,sticky="we")
#text_box.insert(0, "0")
var = StringVar() # Add this line
text_box = Entry(calc, font=("aerial",20,"bold"),bd=45,bg="powder blue",justify=RIGHT, textvariable=var) # Modification here
text_box.grid(row = 0, column = 0, columnspan = 4, pady = 5)
var.set(0) # When you lunch the GUI, you will get 0 in text_box
#text_box.insert(0, "0") <-- You do not need this line
numbers = "789456123"
i = 0
bttn = []
for j in range(1,4):
for k in range(3):
bttn.append(Button(calc, text = numbers[i],padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="seashell3"))#Changes colour of numbers
bttn[i].grid(row = j, column = k, pady = 5)
bttn[i]["command"] = lambda x = numbers[i]: sum1.num_press(x)
i += 1
bttn_0 = Button(calc, text = "0",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="seashell3",command=lambda:sum1.num_press(0)).grid(row = 4, column = 1, pady = 5)
bttn_div = Button(calc, text = chr(247),padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="powder blue",command= lambda: sum1.operation("divide")).grid(row = 1, column = 3, pady = 5)
bttn_mult = Button(calc, text = "x",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="powder blue",command = lambda: sum1.operation("times")).grid(row = 2, column = 3, pady = 5)
minus = Button(calc, text = "-",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="powder blue",command = lambda: sum1.operation("minus")).grid(row = 3, column = 3, pady = 5)
point = Button(calc, text = ".",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="seashell3",command= lambda: sum1.num_press(".")).grid(row = 4, column = 0, pady = 5)
add = Button(calc, text = "+",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="powder blue",command = lambda: sum1.operation("add")).grid(row = 4, column = 3, pady = 5)
neg= Button(calc, text = "+/-",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="powder blue")
neg["command"] = sum1.sign
neg.grid(row = 5, column = 0, pady = 5)
clear = Button(calc, text = "C",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="plum2")
clear["command"] = sum1.cancel
clear.grid(row = 4, column = 2, pady = 5)
all_clear = Button(calc, text = "AC",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="plum2")
all_clear["command"] = sum1.all_cancel
all_clear.grid(row = 5, column = 0, pady = 5)
equals = Button(calc, text = "=",padx=16,bd=8,fg="black",font=("arial",20,"bold"),bg="plum2")
equals["command"] = sum1.calc_total
equals.grid(row = 5, column = 3, pady = 5)
root.mainloop()
