为什么我的按钮不适用于 Tkinter 计算器?

Why my buttons don't work for Tkinter Calculator?

#Calculator codes
from Tkinter import*

class Calculator():
    def __init__ (self):
        self.total = 0
        self.current = ""
        self.newnumber = True
        self.operation = ""
        self.equal = False

    def PressedNumber(self, number):
        self.equal = False
        t = text_box.get()
        n = str(number)
        if self.newnumber:
            self.current = n
            self.newnumber = False
        else:
            if n == '.':
                if n in t:
                    return
                self.current = t + n
        self.Display(self.current)

    def TotalCalculated(self):
        self.equal = True
        self.current = float(self.current)
        if self.operation_pending == True:
            self.calc_sum()
        else:
            self.total = float(text_box.get())

    def Display(self, value):
        text_box.delete(0, END)
        text_box.insert(0, value)

    def calc_sum(self):
        if self.operation == "subtract":
            self.total -= self.current
        if self.operation == "add":
            self.total += self.current
        if self.operation == "divide":
            self.total /= self.current
        if self.operation == "multiply":
            self.total *= self.current
        self.newnumber = True
        self.operation_pending = False
        self.Display(self.total)

    def operation(self, operation):
        self.current = float(self.current)
        if self.operation_pending:
            self.calc_sum()
        elif not self.equal:
            self.total = self.current
        self.newnumber = True
        self.operation_pending = True
        self.operation = operation
        self.equal = False

    def cancel(self):
        self.equal = False
        self.current = "0"
        self.Display(0)
        self.newnumber = True

    def Cancelation_forEverything(self):
        self.cancel()
        self.total = 0

    def sign(self):
        self.equal = False
        self.current = -(float(text_box.get()))
        self.Display(self.current)

summ = Calculator()
root = Tk()
Calculator = Frame(root)
Calculator.grid()

root.title("Calculator")
root.configure(bg="Khaki")
root.minsize(width=220, height=20)
root.resizable(width=FALSE, height= FALSE)
text_box = Entry(Calculator, justify=RIGHT)
text_box.grid(row = 0, column = 0, columnspan=3, pady = 8, sticky=W+E)
text_box.insert(0, "0")

Numbers = "789456123"
a = 0
bttn = []
for r in range(1,4):
    for c in range(3):
        bttn.append(Button(Calculator, text = Numbers[a], font="Candara,20"))
        bttn[a].grid(row = r, column = c, padx= 15, pady = 15)
        bttn[a]["command"] = lambda x = Numbers[a]: summ.PressedNumber(x)
        a += 1

bttn_0 = Button(Calculator, text = "     0      ", font="Candara,20")
bttn_0["command"] = lambda: summ.PressedNumber(0)
bttn_0.grid(columnspan = 5, sticky=N+W, padx= 20, pady = 20)

bttn_division = Button(Calculator, text = chr(247), font="Candara,20")
bttn_division["command"] = lambda: summ.operation("divide")
bttn_division.grid(row = 1, column = 3, pady = 10)

bttn_multiply = Button(Calculator, text = "x", font="Candara,20")
bttn_multiply["command"] = lambda: summ.operation("multiply")
bttn_multiply.grid(row = 2, column = 3, sticky=N, pady = 10)

bttn_subtract = Button(Calculator, text = "-", font="Candara,20")
bttn_subtract["command"] = lambda: summ.operation("subtract")
bttn_subtract.grid(row = 3, column = 3, pady = 10)

bttn_point = Button(Calculator, text = ".", font="Candara,20")
bttn_point["command"] = lambda: summ.PressedNumber(".")
bttn_point.grid(row = 4, column = 1, padx = 10, pady = 10)

bttn_addition = Button(Calculator, text = "+", font="Candara,20")
bttn_addition["command"] = lambda: summ.operation("add")
bttn_addition.grid(row = 4, column = 3, pady = 10)

bttn_neg = Button(Calculator, text = "+/-", font="Candara,20")
bttn_neg["command"] = summ.sign
bttn_neg.grid(row = 5, column = 0, pady = 10)

clear = Button(Calculator, text = "C", font="Candara,20")
clear["command"] = summ.cancel
clear.grid(row = 5, column = 1, pady = 10)

all_clear = Button(Calculator, text = "AC", font="Candara,20")
all_clear["command"] = summ.Cancelation_forEverything
all_clear.grid(row = 5, column = 2, pady = 10)

equals = Button(Calculator, text = "=", font="Candara,20")
equals["command"] = summ.TotalCalculated
equals.grid(row = 5, column = 3, pady = 10)

root.mainloop()

一旦我添加了颜色代码,计算符号就无法工作并给我一个错误...这是我按下其中一个计算符号时的错误消息:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1532, in __call__
    return self.func(*args)
  File "C:\Users\HANY\Documents\Nona CS\Calculator3.py", line 108, in <lambda>
    bttn_multiply["command"] = lambda: summ.operation("multiply")
TypeError: 'str' object is not callable

谁能告诉我该怎么做才能使代码正常工作?

我认为问题在于您在计算器 class 中将 operation 定义为字符串变量:

 self.operation = ""

但是你也把它定义为一个方法:

def operation(self, operation)

因此,当您 summ.operation("multiply") python 调用字符串变量时,而不是方法。当然,这会导致字符串不可调用的错误。