Class has no attribute <obj-name> - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Class has no attribute <obj-name> (/thread-16838.html) |
Class has no attribute <obj-name> - arunprasanth - Mar-17-2019 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 amountTHIS 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' RE: Class has no attribute <obj-name> - metulburr - Mar-17-2019 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() RE: Class has no attribute <obj-name> - arunprasanth - Mar-18-2019 Hi, Thanks for your response, It worked. My bad didn't see how I declared the statements. |