属性错误 python tkinter

Attribute error python tkinter

我正在尝试使用 class 制作一个计算器并且(我对此很陌生)这段代码一直告诉我 AttributeError: 'Calculator' object has no attribute 'clear'and when I 运行 我的代码,class 中的所有内容都不起作用。我该怎么做才能修复我的代码?

class Calculator(Frame):
    def __init__(self):
        Frame.__init__(self)
        display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
        buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
        display.grid(column=0, row=0, padx=0, pady=0)
        buttons.grid(column=0, row=1, padx=1)
        numbers = StringVar()
        results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
        results.pack()
        results.grid(column=0, row=0)

        def showup(x):
            return lambda: results.insert(END, x)

        def equals(self):
            try:
                result = eval(results.get())
            except:
                result = "Invalid input"
                self.all_clear
                results.insert(0, result)
        def zero(self):
            results.insert(END, "0")
        def bracket_one(self):
            results.insert(END, "(")
        def bracket_two(self):
            results.insert(END, ")")
        def all_clear(self):
            results.delete(0, END)
        def clear(self):
            results.delete(-1)
        def multiplication(self):
            results.insert(END, "x")
        def division(self):
            results.insert(END, "/")
        def addition(self):
            results.insert(END, "+")
        def subtraction(self):
            results.insert(END, "-")
        def decimal_point(self):
            results.insert(END, ".")

您在 class Calculator(Frame): 中的缩进是错误的,您在 __init__() 中而不是在 class 中缩进了所有方法。减少 - equals(self)zero(self) 等方法的缩进,并将它们移到 __init__() 之外。

此外,您应该将 results 作为实例变量。并将其作为实例变量访问 - self.results.

例子-

from tkinter import *
from tkinter import messagebox
calculator = Tk()
calculator.title("Calcualtor")
calculator.geometry("317x145")

menubar = Menu(calculator)

class Calculator(Frame):
    def __init__(self):
        Frame.__init__(self)
        display = Frame(calculator, bd=0, width=1000, height=1000, relief=SUNKEN)
        buttons = Frame(calculator, bd=0, width=7, height=1, relief=GROOVE)
        display.grid(column=0, row=0, padx=0, pady=0)
        buttons.grid(column=0, row=1, padx=1)
        numbers = StringVar()
        self.results = Entry(display, textvariable=numbers, width=31, fg="DarkOrchid4", bg="lavender blush", font="Verdana")
        self.results.pack()
        self.results.grid(column=0, row=0)

        def showup(x):
            return lambda: self.results.insert(END, x)

        numbers=["7", "4", "1", "8", "5", "2", "9", "6", "3"]
        for i in range(9):
            n=numbers[i]
            Button(buttons, bg="snow", text=n, width=7, height=1, command=showup(n), relief=RAISED).grid(row=i%3, column=i//3)

        Clear = Button(buttons, bg="snow", text="C", width=7, height=1, command=self.clear, relief=RAISED)
        Clear.grid(padx=2, pady=2, column=3, row=0)
        All_clear = Button(buttons, bg="snow", text="AC", width=7, height=1, command=self.all_clear, relief=RAISED)
        All_clear.grid(padx=2, pady=2, column=4, row=0)
        Bracket_one = Button(buttons, bg="snow", text="(", width=7, height=1, command=self.bracket_one, relief=RAISED)
        Bracket_one.grid(padx=2, pady=2, column=2, row=3)
        Bracket_two = Button(buttons, bg="snow", text=")", width=7, height=1, command=self.bracket_two, relief=RAISED)
        Bracket_two.grid(padx=2, pady=2, column=3, row=3)
        Zero = Button(buttons, bg="snow", text="0", width=7, height=1, command=self.zero, relief=RAISED)
        Zero.grid(padx=2, pady=2, column=0, row=3)
        Decimal_point = Button(buttons, bg="snow", text=".", width=7, height=1, command=self.decimal_point, relief=RAISED)
        Decimal_point.grid(padx=2, pady=2, column=1, row=3)
        Multiplication = Button(buttons, bg="red", text="x", width=7, height=1, command=self.multiplication, relief=RAISED)
        Multiplication.grid(padx=2, pady=2, column=3, row=1)
        Division = Button(buttons, bg="powder blue", text="/", width=7, height=1, command=self.division, relief=RAISED)
        Division.grid(padx=2, pady=2, column=4, row=1)
        Addition = Button(buttons, bg="yellow", text="+", width=7, height=1, command=self.addition, relief=RAISED)
        Addition.grid(padx=2, pady=2, column=3, row=2)
        Subtraction = Button(buttons, bg="green", text="-", width=7, height=1, command=self.subtraction, relief=RAISED)
        Subtraction.grid(padx=2, pady=2, column=4, row=2)
        Equals = Button(buttons, bg="orange", text="=", width=7, height=1, command=self.equals, relief=RAISED)
        Equals.grid(padx=2, pady=2, column=4, row=3)

    def equals(self):
        try:
            result = eval(self.results.get())
        except:
            result = "Invalid input"
        self.all_clear()
        self.results.insert(0, result)
    def zero(self):
        self.results.insert(END, "0")
    def bracket_one(self):
        self.results.insert(END, "(")
    def bracket_two(self):
        self.results.insert(END, ")")
    def all_clear(self):
        self.results.delete(0, END)
    def clear(self):
        self.results.delete(-1)
    def multiplication(self):
        self.results.insert(END, "x")
    def division(self):
        self.results.insert(END, "/")
    def addition(self):
        self.results.insert(END, "+")
    def subtraction(self):
        self.results.insert(END, "-")
    def decimal_point(self):
        self.results.insert(END, ".")

if __name__ == '__main__':
    Calculator().mainloop()
    calculator.config(menu=menubar)
    calculator.mainloop()