Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_plot_polygon.py
- import math
- #change to tkinter for python3
- from Tkinter import *
- #from PIL import Image, ImageDraw
- #import Image, ImageTk
- 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)]
- coord=[]
- mvPt = 0
- mouse_drag = 0
- unit = 20
- def snapto(event):
- return (event.x+unit/2)/unit*unit, (event.y+unit/2)/unit*unit
- def mouse_coords(event):
- x, y = snapto(event)
- if x and y:
- canvas.delete('mouse_pos')
- canvas.create_text(570, 20, text=[x,y], tag='mouse_pos')
- canvas.create_oval(x-6, y-6, x+6, y+6, outline='black', tag='mouse_pos')
- if mvPt:
- list_of_points[mvPt-1] = (x,y)
- func_Draw_polygons()
- def draw_polygons(event):
- x, y = snapto(event)
- if x and y:
- list_of_points.append((x, y))
- func_Draw_polygons()
- def mouse_release(event):
- global mvPt
- mvPt = 0
- print
- print list_of_points
- def move_point(event):
- global mvPt
- x, y = snapto(event)
- if (x,y) in list_of_points:
- mvPt = list_of_points.index((x,y))+1
- def delete_last_point(null):
- if list_of_points:
- list_of_points.pop(-1)
- func_Draw_polygons()
- def func_Draw_polygons():
- canvas.delete('main')
- for pt in list_of_points:
- x, y = pt
- 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, tag='main')
- numberofPoint=len(list_of_points)
- # Draw polygon
- if numberofPoint>2:
- canvas.create_polygon(list_of_points, fill='', outline='green', width=2, tag='main')
- elif numberofPoint==2 :
- canvas.create_line(list_of_points, tag='main')
- # ImageDraw.ImageDraw.polygon((list_of_points), fill=None, outline=None)
- root = Tk()
- canvas = Canvas(root,height=600,width=600)
- canvas.pack()
- root.bind("<Button 1>", draw_polygons)
- root.bind("<ButtonRelease-1>", mouse_release)
- root.bind("<ButtonRelease-3>", mouse_release)
- root.bind("<Button 3>", move_point)
- root.bind("<Motion>", mouse_coords)
- root.bind("<space>", delete_last_point)
- func_Draw_polygons()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement