Python Forum
Attribute error - 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: Attribute error (/thread-11755.html)



Attribute error - RandomCoder - Jul-24-2018

This is what i did
from tkinter import *
import random
import time
root = Tk()

class e():
    def startofgame():
        e.Name=str(name_entry1.get())
        e.carname=str(name_entry2.get())
        b3.grid_remove()
        b1.grid()
        b2.grid()
        tex.grid()
        name_text1.grid_remove()
        name_text2.grid_remove()
        name_text3.grid_remove()
        name_entry1.grid_remove()
        name_entry2.grid_remove()
        root.geometry( '840x275' )
        
    def card():
        e.Name="Example Example"
        e.carname="example"
        e.carseats=2
        e.fuel=80
        e.speed=40
    def customercard():
        e.cName="Example Example"
        e.people=2
        e.m=0


    def order():
        b1.grid_remove()
        b2.grid_remove()
        b4.grid()
        b5.grid()
        e.people=random.randint(0,4)
        e.m=random.randint(0,10)
        if e.people>e.carseats:
            n=n+2
        elif e.people<e.carseats:
            n=n-2
        e.cName=random.randint(0,5)
        if Name==1:
            e.cName="Vanessa Roberts"
        if name==2:
            e.cName="Frank John"
        if name==3:
            e.cName="Mark Holler"
        if name==4:
            e.cName="Arnold Waller"
        if name==5:
            e.cName="Vanessa Roberts"
        if name==6:
            e.cName="Vanessa Roberts"
        if name==7:
            e.cName="Vanessa Roberts"
        if name==8:
            e.cName="Vanessa Roberts"
        if name==9:
            e.cName="Vanessa Roberts"
        if name==10:
            e.cName="Logan paul"
        tex.insert(END,"Amount of People: "+str(e.people)+"\n Minutes: "+str(e.m)+"\n Name: "+str(e.cName)+"/n")

    def takeorder():
        b4.grid_remove()
        b5.grid_remove()
        tex.insert(END,"Driving to destination")
        time.sleep(e.m)
        b1.grid
        b2.grid
        tex.insert(END,"Mision complete")
     

    
b1 = Button(root,text="order", command=e.order ,width=10,height=2)
b2 = Button(root,text="Shop",width=10,height=2)
b3 = Button(root,text="Start", command=e.startofgame ,width=10,height=2)
b4 = Button(root,text="Take order", command=e.order ,width=10,height=2)
b5 = Button(root,text="Change order", command=e.order ,width=10,height=2)
name_text1 = Label(text="Name")
name_text2 = Label(text="Car name")
name_text3 = Label(text="Enter your name \n and your car's name \n (can be made up)")
name_entry1 = Entry()
name_entry2 = Entry()
tex=Text(root,font="14",wrap=WORD,width=50,height=15)


b1.grid(row=0,column=2)
b2.grid(row=5,column=2)
b3.grid(row=4,column=3)
b4.grid(row=2,column=2)
b5.grid(row=4,column=1)
name_text1.grid(row=1, column=2)
name_text2.grid(row=2, column=2)
name_text3.grid(row=0, column=3)
name_entry1.grid(row=1,column=3)
name_entry2.grid(row=2,column=3)
tex.grid(row=0,column=1,rowspan=6)
b1.grid_remove()
b2.grid_remove()
tex.grid_remove()
root.geometry( '240x135' )
b4.grid_remove()
b5.grid_remove()
And this is what I get
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python34\lib\idlelib\run.py", line 121, in main seq, request = rpc.request_queue.get(block=True, timeout=0.05) File "C:\Python34\lib\queue.py", line 175, in get raise Empty queue.Empty During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__ return self.func(*args) File "C:\Users\Krintal\Desktop\Uber tycoon.py", line 40, in order if e.people>e.carseats: AttributeError: type object 'e' has no attribute 'carseats'
Can anyone help?


RE: Attribute error - gontajones - Jul-24-2018

Where are you calling card()?
You have to call it to instantiate the carseats attribute.

Why are you using e. and not self. inside your class?

I also suggest that you use the __init__(self) to instantiate all the attributes