Advertisement
here2share

# Tk_polygon_redraw.py

Oct 5th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. # Tk_polygon_redraw.py
  2.  
  3. # change to tkinter for python3
  4. from Tkinter import *
  5. # from PIL import Image, ImageDraw
  6. # import Image, ImageTk
  7.  
  8. list_of_points=[]
  9. poly = None
  10.  
  11. # Function to get the co-ordianates of  mouse clicked position
  12. def draw_polygons(event):
  13.     mouse_xy = (event.x, event.y)
  14.     func_Draw_polygons(mouse_xy)  
  15.  
  16. # Function to draw polygon
  17. def func_Draw_polygons(mouse_xy):
  18.     global poly, list_of_points
  19.     center_x, center_y = mouse_xy
  20.     canvas.delete(ALL)
  21.  
  22.     list_of_points.append((center_x, center_y))
  23.  
  24.     for pt in list_of_points:
  25.         x, y =  pt
  26.         # draw dot over position which is clicked
  27.         x1, y1 = (x - 1), (y - 1)
  28.         x2, y2 = (x + 1), (y + 1)
  29.         canvas.create_oval(x1, y1, x2, y2, fill='green', outline='green', width=5)
  30.  
  31.     # add clicked positions to list
  32.  
  33.     number_of_points=len(list_of_points)
  34.     # Draw polygon
  35.     if number_of_points > 2:
  36.         poly=canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
  37.     elif number_of_points == 2 :
  38.         print('line')
  39.         canvas.create_line(list_of_points)
  40.     else:
  41.         print('dot')
  42.  
  43.     print(list_of_points)
  44.     print
  45.     print
  46. #
  47.  
  48. root = Tk()
  49. canvas = Canvas(root,height=600,width=600)
  50. canvas.pack()
  51. # bind function to canvas to generate event
  52. canvas.bind("<Button 1>", draw_polygons)
  53. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement