user3015380
user3015380

Reputation: 35

Getting error "No attribute draw"

So I'm trying to use graphics to draw three separate dice and while I have finally gotten them to show up, I'm now getting this error

>>> Game()
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    Game()
  File "C:\Users\Brandon\Desktop\FinalProject3.py", line 19, in Game
    Green.draw(Gamewindow)
AttributeError: 'DieViewGreen' object has no attribute 'draw'

Which literally makes no sense to me because graphics should have imported it.

Is there something missing from my code?

from graphics import *

class DieViewGreen:
    """ DieView is a widget that displays a graphical representation
    of a standard six-sided die."""

    def __init__(self, win, center, value):
        """Create a view of a die, e.g.:
           d1 = GDie(myWin, Point(40,50), 20)
        creates a die centered at (40,50) having sides
        of length 20."""

        # first define some standard values
        self.win = win
        #self.background = Color color of die face
        #self.foreground = Color2 # color of the pips

        # create a square for the face
        if value==0:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green')        

        """ Set this die to display value."""
        if value == 1:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Brain=Text(Point(40,75),'B')
            self.Brain.draw(self.win)


        elif value == 2:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Brain=Text(Point(40,75),'B')
            self.Brain.draw(self.win)

        elif value == 3:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Brain=Text(Point(40,75),'B')
            self.Brain.draw(self.win)


        elif value == 4:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Foot=Text(Point(40,75),'F')
            self.Foot.draw(self.win)


        elif value == 5:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Foot=Text(Point(40,75),'F')
            self.Foot.draw(self.win)


        else:
            x, y = center.getX(), center.getY()
            p1 = Point(x-25, y-25)
            p2 = Point(x+25, y+25)
            rect = Rectangle(p1,p2)
            rect.draw(win)
            rect.setFill('green') 
            self.Shotgun=Text(Point(40,75),'S')
            self.Shotgun.draw(self.win)

The function that I'm running my class in is this, it calls the dieview class in(there are three like the one above) and uses it to draw a window

 from graphics import *
    from DieViewYellow import *
    from DieViewGreen import *
    from DieViewRed import *
    from Button import *
    import random


    def Game():
        """Runs the game"""
        Gamewindow = GraphWin('Game', 200, 200)
        Green = DieViewGreen(Gamewindow, Point(40,75),20)
        Yellow = DieViewYellow(Gamewindow, Point(95,75),20)
        Red = DieViewRed(Gamewindow, Point(150,75),20)
        Green.draw(Gamewindow)
        Roll = Button(Gamewindow, Point(100,130), 160, 20, "Roll Dice")
        Continue = Button(Gamewindow, Point(55,170), 70, 20, "Continue")
        while True:
            pt=Gamewindow.getMouse()
            if roll.clicked(pt):
                DieViewGreen(Gamewindow, Point(40,75),3)
            else:
                continue
            #else:
           #     Exitbutton = "Exit"
           # Stopexit = Button(Gamewindow, Point(145,170), 70, 20, Exitbutton)
          #  Buttons = [Roll, Continue, Stopexit]
           # for i in range(len(Buttons)):
          #      Buttons[i].activate()
          #  n = Gamewindow.getMouse()

Upvotes: 0

Views: 1611

Answers (2)

user3015380
user3015380

Reputation: 35

The issue was within my actual game code and not my die view, as I was using Green.draw when I didn't need to

Upvotes: 0

Nafiul Islam
Nafiul Islam

Reputation: 82550

What that error simply means is that you do not have a draw function inside your DieViewGreen class. Your program expects there to be one.

Upvotes: 2

Related Questions