Python画次摆线错误?
Python drawing a hypotrochoid error?
大家好,我的 Python 项目的绘图功能有问题。我需要绘制一个次摆线,输入由不同函数生成的点列表。我的绘图功能
def draw(points):
win = GraphWin("My Hypotrochoid", 1000, 1000)
hypo = Polygon(points)
hypo.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done
参数"points"是要绘制的x和y坐标列表。我得到的错误:
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok/project1.py", line 75, in draw
hypo = Polygon(points)
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok\graphics.py", line 643, in __init__
self.points = list(map(Point.clone, points))
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok\graphics.py", line 531, in clone
other = Point(self.x,self.y)
AttributeError: 'tuple' object has no attribute 'x'
我在使用项目中另一个函数生成的 x 和 y 坐标数组作为 "points" 参数后出现此错误,我怀疑 Polygon 方法无法将数组识别为输入...不过我可能是错的。
我在这个项目中使用的模块是 John Zelle 的 graphics.py 模块,http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphics.html,因为我们不允许在这个项目中使用海龟图形。
请指出我做错了什么,谢谢大家的帮助!
快速术语吹毛求疵:"array" 不是内置的 Python 类型。除非您导入了 array
模块,否则您几乎肯定没有使用数组。
hypo = Polygon(points)
您似乎正在向此处的多边形初始值设定项发送一个点元组。但是,文档说:
Polygon(point1, point2, point3, ...)
Constructs a polygon having the given points as vertices. Also accepts a single parameter that is a list of the vertices.
看起来它只接受列表,不接受元组。调查源代码证实了这一点。
class Polygon(GraphicsObject):
def __init__(self, *points):
# if points passed as a list, extract it
if len(points) == 1 and type(points[0]) == type([]):
points = points[0]
如果 points[0]
是元组,type(points[0]) == type([])
肯定不起作用。
幸运的是,修复很简单。
hypo = Polygon(list(points))
或者,首先将 points
创建为列表。如果您的代码看起来像 points = (whatever, whatever, whatever)
,请将其更改为 points = [whatever, whatever, whatever]
.
或者,再次使用 argument unpacking,这样 Polygon
初始化程序将您的参数视为单独的项目,而不是一个元组。
hypo = Polygon(*points)
大家好,我的 Python 项目的绘图功能有问题。我需要绘制一个次摆线,输入由不同函数生成的点列表。我的绘图功能
def draw(points):
win = GraphWin("My Hypotrochoid", 1000, 1000)
hypo = Polygon(points)
hypo.draw(win)
win.getMouse() # Pause to view result
win.close() # Close window when done
参数"points"是要绘制的x和y坐标列表。我得到的错误:
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok/project1.py", line 75, in draw
hypo = Polygon(points)
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok\graphics.py", line 643, in __init__
self.points = list(map(Point.clone, points))
File "//uniwa.uwa.edu.au/userhome/students8/21133788/Desktop/CITS1401/2015/Project 1/ok\graphics.py", line 531, in clone
other = Point(self.x,self.y)
AttributeError: 'tuple' object has no attribute 'x'
我在使用项目中另一个函数生成的 x 和 y 坐标数组作为 "points" 参数后出现此错误,我怀疑 Polygon 方法无法将数组识别为输入...不过我可能是错的。
我在这个项目中使用的模块是 John Zelle 的 graphics.py 模块,http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphics.html,因为我们不允许在这个项目中使用海龟图形。
请指出我做错了什么,谢谢大家的帮助!
快速术语吹毛求疵:"array" 不是内置的 Python 类型。除非您导入了 array
模块,否则您几乎肯定没有使用数组。
hypo = Polygon(points)
您似乎正在向此处的多边形初始值设定项发送一个点元组。但是,文档说:
Polygon(point1, point2, point3, ...)
Constructs a polygon having the given points as vertices. Also accepts a single parameter that is a list of the vertices.
看起来它只接受列表,不接受元组。调查源代码证实了这一点。
class Polygon(GraphicsObject):
def __init__(self, *points):
# if points passed as a list, extract it
if len(points) == 1 and type(points[0]) == type([]):
points = points[0]
如果 points[0]
是元组,type(points[0]) == type([])
肯定不起作用。
幸运的是,修复很简单。
hypo = Polygon(list(points))
或者,首先将 points
创建为列表。如果您的代码看起来像 points = (whatever, whatever, whatever)
,请将其更改为 points = [whatever, whatever, whatever]
.
或者,再次使用 argument unpacking,这样 Polygon
初始化程序将您的参数视为单独的项目,而不是一个元组。
hypo = Polygon(*points)