2
  1. 问题

我在 python shell 中遇到了这个错误:

    File "C:\Users\\Desktop\MyPython\ArtemisGraphics1.py", line 619, in draw
        self._buttons[i].draw(self._win)
    File "C:\Users\\Desktop\MyPython\ArtemisGraphics1.py", line 435, in draw
        self.rect.draw(win)
    File "C:\Python27\zellegraphics.py", line 434, in draw
        if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
    GraphicsError:    ('O', 'b', 'j', 'e', 'c', 't', ' ', 'c', 'u', 'r', 'r', 'e', 'n', 't', 'l', 'y', ' ', 'd', 'r', 'a', 'w', 'n')
  1. 编码

这是运行时我收到错误的代码:

 self._x = Button(Point(50,100),20,20,'x')
 self._square = ButtonSqr(Point(50,50),4,20,20,win)
 self._square.draw()
 self._x.draw(win)
 if self._x.clicked:
     self._square.undraw()

ZelleGraphics 中与错误有关的代码:

     def draw(self, graphwin):

    """Draw the object in graphwin, which should be a GraphWin
    object.  A GraphicsObject may only be drawn into one
    window. Raises an error if attempt made to draw an object that
    is already visible."""

    if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
    if graphwin.isClosed(): raise GraphicsError("Can't draw to closed window")
    self.canvas = graphwin
    self.id = self._draw(graphwin, self.config)
    if graphwin.autoflush:
        _root.update()

这是可能的相关代码:

    class ButtonSqr ():
    def __init__(self, center, numberOfButtons, width, height, win):
        self._win = win
        x = center.getX()
        y = center.getY()
        self._numberOfButtons = numberOfButtons
        self._reaction = None
        labels = [None, None, None, None]
        self._b1 = Button(center, width, height, win)
        self._b2 = Button(zg.Point(x+width,y), width, height, win)
        self._b3 = Button(zg.Point(x+width, y+height), width, height, win)
        self._b4 = Button(zg.Point(x, y+height), width, height, win)
        self._buttons = [self._b1,self._b2,self._b3,self._b4]
    def draw(self):
        for i in range(0, self._numberOfButtons):
            self._buttons[i].draw(self._win)
    def undraw(self):
        for i in range(0, self._numberOfButtons):
            self._buttons[i].undraw()
    def setLabels(self, textList):
        labels = [None, None, None, None]
        for i in range(len(textList)):
            labels[i] = textList[i]
        self._b1.setLabel(labels[0])
        self._b2.setLabel(labels[1])
        self._b3.setLabel(labels[2])
        self._b4.setLabel(labels[3])
    def setTextColor(self, color):
        self._b1.setTextColor(color)
        self._b2.setTextColor(color)
        self._b3.setTextColor(color)
        self._b4.setTextColor(color)
    def setFill(self, color):
        self._b1.setFill(color)
        self._b2.setFill(color)
        self._b3.setFill(color)
        self._b4.setFill(color)
    def waitForClick(self):
        clicked = False
        while clicked == False:
            if self._b1.clicked(self._win.getMouse()):
                clicked = True
                button = self._b1
            elif self._b2.clicked(self._win.getMouse()):
                clicked = True
                button = self._b2
            elif self._b3.clicked(self._win.getMouse()):
                clicked = True
                button = self._b3
            elif self._b4.clicked(self._win.getMouse()):
                clicked = True
                button = self._b4
        return button.getLabel()        
    def checkClick(self, point):
        if self._b1.clicked(point):
            self._reaction = self._b1.getLabel()
            return True
        elif self._b2.clicked(point):
            self._reaction = self._b2.getLabel()
            return True
        elif self._b3.clicked(point):
            self._reaction = self._b3.getLabel()
            return True
        elif self._b4.clicked(point):
            self._reaction = self._b4.getLabel()
            return True
        else:
            return False
    def getReaction(self):
        return self._reaction
4

0 回答 0