Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AttributeError:Rectangle实例没有属性';y';_Python_Attributes - Fatal编程技术网

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

Python AttributeError:Rectangle实例没有属性';y';,python,attributes,Python,Attributes,我正在编写一个程序,其中用户绘制一个矩形,然后计算周长和面积。在查找两侧的长度时,我得到了一个AttributeError:Rectangle实例没有属性“y” 以下是我目前的代码: from graphics import* win=GraphWin('Rectangle',800,800) def twopoints(): pRight=win.getMouse() pRight.draw(win) pLeft=win.getMouse() pLeft.d

我正在编写一个程序,其中用户绘制一个矩形,然后计算周长和面积。在查找两侧的长度时,我得到了一个
AttributeError:Rectangle实例没有属性“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)

返回多件物品的顺序很重要。给你

return pRight, pLeft, rec
然后把它分配给

rec, pRight, pLeft=twopoints()
Python在这里不按名称匹配,只按顺序匹配,因此
pLeft
外部实际上与
rec
内部相同