Python Forum
AttributeError: 'Calculator' object has no attribute 'buttons'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'Calculator' object has no attribute 'buttons'
#1
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()
Reply
#2
add:
self.buttons = []
to your init routine. you can still set it on line 17 without harm, but not necessary.
Reply
#3
In the code you posted, you have to call __createButtons for self.buttons to be created, which is never done.
Reply
#4
You never call __createButtons() or __createDisplay(), which do what looks like initialization code.
Reply
#5
Still kinda lost here. What exactly do I have to do to call these in?
Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,672 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,490 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 748 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,867 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,333 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,593 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,901 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,475 Jul-29-2022, 09:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020