1

I am trying to make a calculator using a class and (Im quite new to this) this code keeps telling me AttributeError: 'Calculator' object has no attribute 'clear'and when I run my code, everything inside the class doesn't work. What can I do to fix my code?

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, ".")
2
  • You should put all your functions outside of the __init__() function. Commented Aug 26, 2015 at 7:50
  • I moved them all outside but now my eval function doesn't work, everytime I type in my equation and press equals it always says "Invalid input" plus whatever I just typed in Commented Aug 26, 2015 at 8:25

1 Answer 1

1

Your indentation in the class Calculator(Frame): is wrong, you have indented all the methods inside __init__() instead of inside the class. Decrease the indentation for the methods like - equals(self) , zero(self) , etc , and move them outside __init__() .

Also, you should put results as an instance variable. And access it as an instance variable - self.results.

Example -

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()
Sign up to request clarification or add additional context in comments.

11 Comments

how come my eval doesn't work anymore? I followed this exactly and whenever I enter numbers in, my calc gives me invalid input when i press equals
You are just getting the result you would need to set it back to self.results
im sorry but i don't understand... I thought that my eval would take care of the calculations? What would I need to do for it to calculate entries again?
it works now but it doesn't clear out the equation i just entered for example if i enter 63 + 2 then hit "=" it gives me 6563 +2
No, actually the indentation was a bit wrong there as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.