得到错误“没有属性绘制”

时间:2013-12-07 05:26:03

标签: python graphics python-3.x draw

所以我试图使用图形来绘制三个独立的骰子,而我终于让它们出现了,我现在收到这个错误

>>> 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'

这对我来说毫无意义,因为图形应该导入它。

我的代码中是否缺少某些内容?

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)

我正在运行我的类的函数就是这个,它调用了dieview类(有三个像上面那个)并用它来绘制一个窗口

 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()

2 个答案:

答案 0 :(得分:2)

该错误只是意味着您的draw课程中没有DieViewGreen功能。你的程序希望有一个。

答案 1 :(得分:0)

问题出在我的实际游戏代码中,而不是我的死视图,因为我在不需要时使用Green.draw