AttributeError:Rectangle实例没有属性' y'

时间:2017-05-31 01:05:14

标签: python attributes

我正在编写一个用户绘制矩形的程序,然后计算周长和面积。在找到双方的长度时,我得到AttributeError: Rectangle instance has no attribute 'y'

到目前为止,这是我的代码:

from graphics import*

win=GraphWin('Rectangle',800,800)

def twopoints():
    pRight=win.getMouse()
    pRight.draw(win)
    pLeft=win.getMouse()
    pLeft.draw(win)
    print (pLeft)
    print (pRight)

    rec=Rectangle(pRight,pLeft)
    rec.draw(win)
    rec.setFill('red')
    return pRight, pLeft, rec

p1=Point(400, 100) 
m=Text(p1,"Click two places on the screen to create a rectangle")
m2=Text(Point(400, 150), "Your first click will be the upper right corner")
m3=Text(Point(400, 200), "Your second click will be the bottom left corner")
m.draw(win)
m2.draw(win)
m3.draw(win)

rec, pRight, pLeft=twopoints()

m.setText("Click to get either the area or perimeter")
m2.undraw()
m3.undraw()

side1=abs(pLeft.y-pRight.y)
side2=abs(pLeft.x-pRight.x)
print (side1)

1 个答案:

答案 0 :(得分:0)

您返回多个内容的顺序非常重要。你在这里

return pRight, pLeft, rec

然后将其分配给

rec, pRight, pLeft=twopoints()

Python在这里按名称不匹配,只是按顺序排列,因此pLeft外部实际上与内部的rec相同。