Python绘制次摆线错误?

标签 python graphics draw

大家好,我的 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 坐标数组作为“点”参数后,我收到此错误,我怀疑 Polygon 方法无法将数组识别为输入...我可能是错的不过。

我在这个项目中使用的模块是 John Zelle 的graphics.py 模块,http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphics.html ,因为我们不允许在这个项目中使用海龟图形。

请让我知道我做错了什么,谢谢大家的帮助!

最佳答案

快速术语挑剔:“数组”不是内置的 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绘制次摆线错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29822081/

相关文章:

python - 使用 AJAX 时 wtforms 验证不起作用

python - 检测停止进程Python

java - 尝试用 JPanel 绘制一个简单的矩形,但没有显示任何内容

java - 用 Canvas 绘制人物并设置其角度的最佳方法是什么?

exception - 将 TextureAddressMode 设置为 Clamp for XNA Reach

java - 如何对图像执行 'undraw' 操作?

python - 排除基于 0 的索引中的最后一个元素

python - 一次读取 1mb 的原始文件,然后在 Python 中执行 Regex

c# - 在 C# 中的图片框上绘制箭头

javascript - 在 Canvas 上绘制具有 2 个点的图像