Python Forum

Full Version: AttributeError: 'Calculator' object has no attribute 'buttons'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
First off this is my first post on this community, I apologize in advance for any mistakes my post may have. Aside from that I am really enjoying Python and look forward to being apart of this forum.

Anyways right now I am making a calculator program to practice coding. I seem to be have issues with the buttons though.
On my shell I keep getting
"AttributeError: 'Calculator' object has no attribute 'buttons'"
-line 43, in getButton
for b in self.buttons:

-line 64, in run
key = self.getButton()

-line 69, in <module>
theCalc.run()

I would really appreciate some help with figuring this out. I am stuck on this part of my book.

My code is this:

from graphics import*
from button import Button

class Calculator:
    def __init__(self):
        win = GraphWin("calculator")
        win.setCoords(0,0,6,7)
        win.setBackground("slategray")
        self.win = win
    

    def __createButtons(self):
        bSpecs = [(2,1,'0'),(3,1,'.'),
                  (1,2,'1'),(2,2,'2'),(3,2,'3'),(4,2,'+'),(5,2,'-'),
                  (1,3,'4'),(2,3,'5'),(3,3,'6'),(4,3,'*'),(5,3,'/'),
                  (1,4,'7'),(2,4,'8'),(3,4,'9'),(4,4,'<-'),(5,4,'C')]
        self.buttons = []
        for(cx,cy,label) in bSpecs:
            self.buttons.append(Button(self.win.Point(cx,cy),
                                       .75,.75,label))

        self.buttons.append(Button(self.win,Point(4.5,1),
                                   1.75,.75,"="))
        for b in self.buttons:
            b.activate()

    def __createDisplay(self):
        bg = Rectangle(Point(.5,5.5), Point(5.5,6.5))
        bg.setFill('white')
        bg.draw(self.win)
        text = Text(Point(3,6),"")
        text.draw(self.win)
        text.setFace("courier")
        text.setStyle("bold")
        text.setSize(16)
        self.display = text

    def getButton(self):

        while True:
            p = self.win.getMouse()
            for b in self.buttons:
                if b.clicked(p):
                    return b.getLabel()

    def processButton(self,key):
        text = self.display.getText()
        if key == 'C':
            self.display.setText("")
        elif key == '<-':
            self.display.setText(text[:-1])
        elif key =='=':
            try:
                result = eval(text)
            except:
                result = 'ERROR'
            self.display.setText(str(result))
        else:
            self.display.setText(text+key)

    def run(self):
        while True:
            key = self.getButton()
            self.processButton(key)

if __name__=='__main__':
    theCalc=Calculator()
    theCalc.run()
add:
self.buttons = []
to your init routine. you can still set it on line 17 without harm, but not necessary.
In the code you posted, you have to call __createButtons for self.buttons to be created, which is never done.
You never call __createButtons() or __createDisplay(), which do what looks like initialization code.
Still kinda lost here. What exactly do I have to do to call these in?
(Apr-18-2018, 01:26 AM)Gomez2021 Wrote: [ -> ]Still kinda lost here. What exactly do I have to do to call these in?
The start of your program.