【问题标题】:How to create polygon on mouse clicks on image in canvas tkinter python如何在画布tkinter python中的鼠标点击图像上创建多边形
【发布时间】:2018-06-26 07:49:44
【问题描述】:

任务是通过鼠标点击图像上的不同位置在图像上创建多个多边形。我使用 Python tkinter 来完成这项任务。我向用户显示了图像。当用户点击图像时,我得到了 x,y 坐标。如何从用户单击的不同坐标创建多边形。最初,我想在鼠标单击时创建简单的线,但随着所选点数的增加,它应该创建多边形。这是我为此任务编写的部分代码。

# Function to get the co-ordianates of  mouse clicked position and draw polygons
def draw_plygons(event):
    mouse_xy = (event.x, event.y)

# Draw canvas for iput image to pop up image for clicks
    filename = ImageTk.PhotoImage(img)
    canvas = Canvas(root,height=img.size[0],width=img.size[0])
    canvas.image = filename
    canvas.create_image(0,0,anchor='nw',image=filename)
    canvas.pack()
# bind function to canvas to generate event
    canvas.bind("<Button 3>", draw_polygons)
    root.mainloop()

【问题讨论】:

    标签: python image tkinter tkinter-canvas


    【解决方案1】:

    这是解决方案,我是如何做到的。

    def draw_polygons(event):
            mouse_xy = (event.x, event.y)
    

    我收集了list_of_points列表中的所有点击点,然后使用:

    canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
    

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2020-11-28
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多