Advertisement
here2share

# Tk_plot_polygon.py

Feb 13th, 2021
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. # Tk_plot_polygon.py
  2.  
  3. import math
  4. #change to tkinter for python3
  5. from Tkinter import *
  6. #from PIL import Image, ImageDraw
  7. #import Image, ImageTk
  8.  
  9. list_of_points=[(20, 20), (20, 120), (100, 60), (40, 280), (120, 280), (140, 60), (160, 80), (140, 280), (200, 280), (200, 180), (220, 280), (300, 280), (220, 160), (280, 100), (220, 100), (200, 140), (220, 40), (180, 60), (200, 20)]
  10.  
  11. coord=[]
  12. mvPt = 0
  13. mouse_drag = 0
  14. unit = 20
  15.  
  16. def snapto(event):
  17.     return (event.x+unit/2)/unit*unit, (event.y+unit/2)/unit*unit
  18.  
  19. def mouse_coords(event):
  20.     x, y = snapto(event)
  21.     if x and y:
  22.         canvas.delete('mouse_pos')
  23.         canvas.create_text(570, 20, text=[x,y], tag='mouse_pos')
  24.         canvas.create_oval(x-6, y-6, x+6, y+6, outline='black', tag='mouse_pos')
  25.         if mvPt:
  26.             list_of_points[mvPt-1] = (x,y)
  27.         func_Draw_polygons()
  28.  
  29. def draw_polygons(event):
  30.     x, y = snapto(event)
  31.     if x and y:
  32.         list_of_points.append((x, y))
  33.         func_Draw_polygons()
  34.  
  35. def mouse_release(event):
  36.     global mvPt
  37.     mvPt = 0
  38.     print
  39.     print list_of_points
  40.  
  41. def move_point(event):
  42.     global mvPt
  43.     x, y = snapto(event)
  44.     if (x,y) in list_of_points:
  45.         mvPt = list_of_points.index((x,y))+1
  46.  
  47. def delete_last_point(null):
  48.     if list_of_points:
  49.         list_of_points.pop(-1)
  50.         func_Draw_polygons()
  51.  
  52. def func_Draw_polygons():
  53.     canvas.delete('main')
  54.  
  55.     for pt in list_of_points:
  56.         x, y =  pt
  57.         x1, y1 = (x - 1), (y - 1)
  58.         x2, y2 = (x + 1), (y + 1)
  59.         canvas.create_oval(x1, y1, x2, y2, fill='green', outline='green', width=5, tag='main')
  60.  
  61.     numberofPoint=len(list_of_points)
  62.     # Draw polygon
  63.     if numberofPoint>2:
  64.         canvas.create_polygon(list_of_points, fill='', outline='green', width=2, tag='main')
  65.     elif numberofPoint==2 :
  66.         canvas.create_line(list_of_points, tag='main')
  67.  
  68.     # ImageDraw.ImageDraw.polygon((list_of_points), fill=None, outline=None)
  69.    
  70. root = Tk()
  71.  
  72. canvas = Canvas(root,height=600,width=600)
  73. canvas.pack()
  74.  
  75. root.bind("<Button 1>", draw_polygons)
  76. root.bind("<ButtonRelease-1>", mouse_release)
  77. root.bind("<ButtonRelease-3>", mouse_release)
  78. root.bind("<Button 3>", move_point)
  79. root.bind("<Motion>", mouse_coords)
  80. root.bind("<space>", delete_last_point)
  81.  
  82. func_Draw_polygons()
  83.  
  84. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement