Python Forum
Class has no attribute <obj-name>
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class has no attribute <obj-name>
#1
Hi, I'am new to Python - I was practicing some coding and got into error:AttributeError: 'ResturantUI' object has no attribute 'resturant'

Here's the code:
 from tkinter import *
from chapter9 import resturant
class ResturantUI(Frame):
    def __init__(self,master):
        super(ResturantUI, self).__init__(master)
        self.grid()
        self.create_widget()
        self.resturant = resturant.Resturant('item1',2)
        self.resturant.add_item('item2',3)

    def create_widget(self):
        Label(self,text="ResturantUI").grid(row=0,column=0,columnspan=1,sticky=W)
        Label(text="Here's the menu!...").grid(row=1,column=0,columnspan=3,sticky=W)
        Entry(text=self.resturant.__str__(self)).grid(row=2,column=1,columnspan=3,sticky=W)
root = Tk()
root.title('REsturantUI')
root.geometry('200x100')
app = ResturantUI(root)
root.mainloop() 
# Following is the code of resturant
class Resturant():
    def __init__(self,item,price):
        # dictionary to store item and price
        self.d = {}

        self.item = item
        self.price = price
        self.d[item] = price

    def __str__(self):
        rep = ""
        if self.d.items():
            for item in self.d.keys():
                rep += item +':\t'+str(self.d[item])+'\n'
            if self.total:
                rep += 'Your bill is: '+str(self.total)
            return rep
        else:
            rep = "<EMPTY>"
            return rep
    def add_item(self,item,price):
        self.d.__setitem__(item,price)

    def remove_item(self,item):
        if self.d.__contains__(item):
            self.d.pop(item)
    @property
    def total(self):
        amount = 0
        for k in self.d.keys():
            amount += self.d.get(k)
        return amount 
THIS IS THE ERROR AM FACING..
Entry(text=self.resturant.__str__(self)).grid(row=2,column=1,columnspan=3,sticky=W)
AttributeError: 'ResturantUI' object has no attribute 'resturant'
Reply
#2
create_widget in which is using self.resturant is before the creation of self.resturant. Switch them around.
        self.resturant = resturant.Resturant('item1',2)
        self.resturant.add_item('item2',3)
        self.create_widget()
Recommended Tutorials:
Reply
#3
Hi, Thanks for your response, It worked. My bad didn't see how I declared the statements.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Initiating an attribute in a class __init__: question billykid999 8 1,248 May-02-2023, 09:09 PM
Last Post: billykid999
  class, attribute and method Frankduc 9 2,381 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Python generics: How to infer generic type from class attribute? Thoufak 0 2,770 Apr-25-2021, 09:31 AM
Last Post: Thoufak
  AttributeError class object has no attribute list object scttfnch 5 3,344 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  "can't set attribute" on class DreamingInsanity 2 10,758 Aug-22-2020, 07:57 PM
Last Post: DreamingInsanity
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 4,073 Dec-03-2018, 11:16 PM
Last Post: Windspar
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 5,695 Jun-19-2018, 02:44 PM
Last Post: jdrp
  Empty attribute class dictionary after saving it in a class object dictionary 3dimensions 6 4,795 May-20-2018, 01:57 PM
Last Post: 3dimensions
  setting base class attribute bb8 1 2,487 Feb-13-2018, 06:37 PM
Last Post: nilamo
  Class attribute not recognized\working J125 1 5,299 Dec-19-2016, 01:05 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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