title: Tkinter update polygon points on mouse click
body: <p>I want to update the polygon on each mouse click. The below code redraw new polygon when new position is obtained from mouse click. I want to update the polygon or get new one (delete old one). How to do it. Here is the complete as suggested. Tkinter library of python is used.  </p>
  
  <pre><code>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&gt;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("&lt;Button 3&gt;", draw_polygons)
          root.mainloop()
  </code></pre>