Tkinter在单击鼠标时更新多边形点

时间:2018-06-26 14:03:16

标签: python image tkinter tkinter-canvas

我想在每次单击鼠标时更新多边形。通过鼠标单击获得新位置时,以下代码重新绘制新多边形。我想更新多边形或获取新的多边形(删除旧的多边形)。怎么做。这是完整的建议。使用python的Tkinter库。

import math
from Tkinter import *
from PIL import Image, ImageDraw
import Image, ImageTk

coord=[]  # for saving coord of each click position

Dict_Polygons={}   # Dictionary for saving polygons
list_of_points=[]

# Function to get the co-ordianates of  mouse clicked position
def draw_polygons(event):
    mouse_xy = (event.x, event.y)
    func_Draw_polygons(mouse_xy)  


# Function to draw polygon
def func_Draw_polygons(mouse_xy):
    center_x, center_y = mouse_xy

    #draw dot over position which is clicked
    x1, y1 = (center_x - 1), (center_y - 1)
    x2, y2 = (center_x + 1), (center_y + 1)
    canvas.create_oval(x1, y1, x2, y2, fill='green', outline='green', width=5)

    # add clicked positions to list
    list_of_points.append(mouse_xy)

    numberofPoint=len(list_of_points)
    # Draw polygon
    if numberofPoint>2:

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

    elif numberofPoint==2 :
        print('line')
        canvas.create_line(list_of_points)
    else:
        print('dot')


  #  ImageDraw.ImageDraw.polygon((list_of_points), fill=None, outline=None)

    print(list_of_points)

##########################################################################
# Main function
if __name__ == '__main__':

        root = tk.Tk()
    # Input image
        img = Image.open("e.png")

    # Draw canvas for input 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()

1 个答案:

答案 0 :(得分:0)

这是否使您更接近解决方案?每次将新点添加到列表时,它都会根据点列表重新绘制。

import math
#change to tkinter for python3
from Tkinter import *
#from PIL import Image, ImageDraw
#import Image, ImageTk

coord=[]  # for saving coord of each click position

Dict_Polygons={}   # Dictionary for saving polygons
list_of_points=[]
poly = None

# Function to get the co-ordianates of  mouse clicked position
def draw_polygons(event):
    mouse_xy = (event.x, event.y)
    func_Draw_polygons(mouse_xy)  


# Function to draw polygon
def func_Draw_polygons(mouse_xy):
    global poly, list_of_points
    center_x, center_y = mouse_xy
    canvas.delete(ALL)

    list_of_points.append((center_x, center_y))

    for pt in list_of_points:
        x, y =  pt
        #draw dot over position which is clicked
        x1, y1 = (x - 1), (y - 1)
        x2, y2 = (x + 1), (y + 1)
        canvas.create_oval(x1, y1, x2, y2, fill='green', outline='green', width=5)

    # add clicked positions to list


    numberofPoint=len(list_of_points)
    # Draw polygon
    if numberofPoint>2:
        poly=canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
    elif numberofPoint==2 :
        print('line')
        canvas.create_line(list_of_points)
    else:
        print('dot')


  #  ImageDraw.ImageDraw.polygon((list_of_points), fill=None, outline=None)

    print(list_of_points)

##########################################################################
# Main function
if __name__ == '__main__':
    root = Tk()


    canvas = Canvas(root,height=200,width=200)

    canvas.pack()
    # bind function to canvas to generate event
    canvas.bind("<Button 3>", draw_polygons)
    root.mainloop()