为什么在执行 Python Tkinter 计算器的代码时出现错误?

Why I get Errors when I execute the codes for a Python Tkinter Calculator?

一开始有人建议我区分name操作下的两个代码..所以,我把self.operation改成了self.operation1。之后我得到另一个错误 self.operatio1_pending 并且建议我在 init() 中引入 self.operation_pending。但是我仍然遇到另一个错误。 这是带有主题标签的书面代码,让您知道我在哪里进行了更改(我在 init() 中添加了 operation="" 因为它给了我一个未定义的错误)注意:我也将不同方法中的所有 self.operation 更改为 self.operation1.

from Tkinter import*

class Calculator():
    def __init__ (self):
        self.total = 0
        self.current = ""
        self.newnumber = True
        self.operation1 = ""#
        self.operation1_pending = False#
        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.operation1_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.operation1 == "subtract":
            self.total -= self.current
        if self.operation1 == "add":
            self.total += self.current
        if self.operation1 == "divide":
            self.total /= self.current
        if self.operation1 == "multiply":
            self.total *= self.current
        self.newnumber = True
        self.operation1_pending = False
        self.Display(self.total)

    def operation(self, operation1):
        self.current = float(self.current)
        if self.operation1_pending:
            self.calc_sum()
        elif not self.equal:
            self.total = self.current
        self.newnumber = True
        self.operation1_pending = True
        self.operation1 = 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 "Q:\Python.001\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "\centre-fsrv07\SSHome$2767933\Grade 10 CS\Working progress\Tkinter\Calculator4.py", line 110, in <lambda>
    bttn_multiply["command"] = lambda: summ.operation("multiply")
  File "\centre-fsrv07\SSHome$2767933\Grade 10 CS\Working progress\Tkinter\Calculator4.py", line 60, in operation
    self.operation1 = operation
NameError: global name 'operation' is not defined

请通过完整的解释和代码帮助我解决我需要更改的内容...如果您知道在没有给定代码的情况下使计算器工作的其他方法,请提出建议,我将不胜感激。

来自您的代码:

def operation(self, operation1):
    self.current = float(self.current)
    if self.operation1_pending:
        self.calc_sum()
    elif not self.equal:
        self.total = self.current
    self.newnumber = True
    self.operation1_pending = True
    self.operation1 = operation     # <-- You mean operation1
    self.equal = False

改变

self.operation1 = operation

self.operation1 = operation1

反映您方法的参数。