from tkinter import *
class Countinator():
def __init__(self, parent):
self.total = 0
self.current = StringVar()
self.new_num = True
self.parent=parent
self.operator_pending = False #Runs when the class Countinator is called. Sets all the variables to begin with
self.equated = False
self.entry= Entry(parent, justify=RIGHT)
self.entry.grid(row = 0, column = 0, columnspan = 4, pady = 5)
self.entry.insert(0, "0")
self.parent.bind("0",lambda event : self.num_press(self, "0"))
self.parent.bind("1",lambda event : self.num_press(self, "1"))
self.parent.bind("2",lambda event : self.num_press(self, "2"))
self.parent.bind("3",lambda event : self.num_press(self, "3"))
self.parent.bind("4",lambda event : self.num_press(self, "4"))
self.parent.bind("5",lambda event : self.num_press(self, "5"))
self.parent.bind("6",lambda event : self.num_press(self, "6"))
self.parent.bind("7",lambda event : self.num_press(self, "7"))
self.parent.bind("8",lambda event : self.num_press(self, "8"))
self.parent.bind("9",lambda event : self.num_press(self, "9"))
self.parent.bind("+",lambda event : sum1.operation("but_addition"))
self.parent.bind("-",lambda event : sum1.operation("but_minus"))
self.parent.bind("*",lambda event : sum1.operation("but_multiply"))
self.parent.bind("/",lambda event : sum1.operation("but_divide"))
self.parent.bind(".",lambda event : sum1.mouse_press("."))
self.parent.bind("<Return>", sum1.calculate_total2)
self.parent.bind("<Delete>", lambda event : sum1.clear2)
self.parent.bind("<BackSpace>", lambda event : sum1.backspace2)
def num_press(self, event, num):
self.equated = False
temporary = self.entry.get()
temporary2 = num
print(self.entry.get())
if self.new_num:
self.current = temporary2 #Looks for when a number is pressed
self.new_num = False
else:
if temporary2 == '.':
if temporary2 in temporary:
return temporary
self.current = temporary + temporary2
self.display(self.current)
def mouse_press(self, num):
self.equated = False
temporary = self.entry.get()
temporary2 = str(num)
if self.new_num:
self.current = temporary2 #Looks for when a number is pressed
self.new_num = False
else:
if temporary2 == '.':
if temporary2 in temporary:
return
self.current = temporary + temporary2
self.display(self.current)
def calculate_total(self):
self.equated = True
self.current = float(self.current)
if self.operator_pending == True: #Called when the equals key is pressed
self.do_sum()
else:
self.total = float(self.entry.get())
def calculate_total2(self, event):
self.equated = True
self.current = float(self.current)
if self.operator_pending == True: #Called when the equals key is pressed
self.do_sum()
else:
self.total = float(self.entry.get())
def display(self, value):
self.entry.delete(0, END) #Displays the numbers from the label
self.entry.insert(0, value)
def do_sum(self):
if self.operator == "but_addition":
self.total += self.current
if self.operator == "but_minus":
self.total -= self.current
if self.operator == "but_multiply": #Does the multiply, addition, subtraction and division
self.total *= self.current
if self.operator == "but_divide":
self.total /= self.current
self.new_num = True
self.operator_pending = False
self.display(self.total)
def operation(self, operator):
self.current = float(self.current)
if self.operator_pending:
self.do_sum()
elif not self.equated:
self.total = self.current
self.new_num = True
self.operator_pending = True
self.operator = operator
self.eq = False
def backspace(self):
print("backspace mouse")
def backspace2(self, event):
print("backspace key")
def clear(self):
self.equated = False
self.current = "0" #Backspace only one character from the label
self.display(0)
self.new_num = True
def clear2(self, event):
self.equated = False
self.current = "0" #Backspace only one character from the label
self.display(0)
self.new_num = True
class GUI:
def __init__(self, parent):
self.numbers=["7","8","9","4","5","6","1","2","3","0"]
self.parent=parent
i=0
self.buttons = []
for rows in range(1,4):
for columns in range(3): #Create the 1-9 buttons and binds them to the button
self.buttons.append(Button( text = self.numbers[i], relief=RIDGE, padx=24, pady=24))
self.buttons[i].grid(row = rows, column = columns, padx=2, pady=2)
self.buttons[i]["command"] = lambda event = self.numbers[i]: sum1.mouse_press(event)
i += 1
self.buttons.append(Button(parent, text = self.numbers[9], relief=RIDGE, padx=24, pady=24))
self.buttons[9].grid(padx=2, pady=2,row=5, column=0,columnspan=2, sticky=W+E)
self.buttons[9]["command"]=lambda event=self.numbers[9]: sum1.mouse_press(0)
self.buttons[0].grid(row=2, column=0, padx=2, pady=2)
self.buttons[1].grid(row=2, column=1, padx=2, pady=2)
self.buttons[2].grid(row=2, column=2, padx=2, pady=2)
self.buttons[3].grid(row=3, column=0, padx=2, pady=2)
self.buttons[4].grid(row=3, column=1, padx=2, pady=2)
self.buttons[5].grid(row=3, column=2, padx=2, pady=2) #Sets the buttons to a set place in the grid
self.buttons[6].grid(row=4, column=0, padx=2, pady=2)
self.buttons[7].grid(row=4, column=1, padx=2, pady=2)
self.buttons[8].grid(row=4, column=2, padx=2, pady=2)
but_multiply=Button(parent, text="*", relief=RIDGE, padx=24, pady=24)
but_multiply.grid(row=2, column=3, padx=2, pady=2)
but_multiply["command"] = lambda: sum1.operation("but_multiply")
but_divide=Button(parent, text="/", relief=RIDGE, padx=24, pady=24)
but_divide.grid(row=2, column=4, padx=2, pady=2)
but_divide["command"] = lambda: sum1.operation("but_divide")
but_minus=Button(parent, text="-", height=6, relief=RIDGE, padx=24, pady=24)
but_minus.grid(row=3, column=3, rowspan=2, padx=2, pady=2)
but_minus["command"] = lambda: sum1.operation("but_minus")
but_addition=Button(parent, height=6, text="+", relief=RIDGE, padx=24, pady=24)
but_addition.grid(row=3, column=4, rowspan=2, padx=2, pady=2)
but_addition["command"] = lambda: sum1.operation("but_addition")
but_equals=Button(parent, width=11, text="=", relief=RIDGE, padx=24, pady=24) #The special characters that get called when the keys are pressed
but_equals.grid(row=5, column=3, padx=2, pady=2, columnspan=2)
but_equals["command"] = sum1.calculate_total
but_decimal=Button(parent, text=".", relief=RIDGE, padx=24, pady=24)
but_decimal.grid(row=5, column=2, padx=2, pady=2)
but_decimal["command"] = lambda: sum1.mouse_press(".")
but_backspace=Button(parent, text="Backspace", relief=RIDGE, padx=13, pady=24, width=24)
but_backspace.grid(row=1, column=2, padx=2, pady=2, columnspan=3)
but_backspace["command"] = sum1.backspace
but_clear=Button(parent, text="Clear", relief=RIDGE, padx=13, pady=24, width=14)
but_clear.grid(row=1, column=0, padx=2, pady=2, columnspan=2)
but_clear["command"] = sum1.clear
root=Tk()
sum1=Countinator(root)
calculator=Frame()
calculator.grid()
gui=GUI(root)
gui=Frame()
gui.grid()
root.title("Calculator")
root.mainloop()
这是我为学校项目制作的计算器的所有代码。
Traceback (most recent call last):
File "D:\12 Digi\Python\Portable Python 3.2.1.1\App\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
File "D:\12 Digi\Python 2014\Calculator\Combined.py", line 86, in calculate_total2
self.do_sum()
File "D:\12 Digi\Python 2014\Calculator\Combined.py", line 105, in do_sum
self.display(self.total)
File "D:\12 Digi\Python 2014\Calculator\Combined.py", line 91, in display
self.entry.delete(0, END) #Displays the numbers from the label
File "D:\12 Digi\Python\Portable Python 3.2.1.1\App\lib\tkinter\__init__.py", line 2372, in delete
self.tk.call(self._w, 'delete', first, last)
_tkinter.TclError: invalid command name ".82911024"
这是我按下返回键时收到的错误消息。我不知道它意味着什么或如何解决它。