Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_polygon_redraw.py
- # change to tkinter for python3
- from Tkinter import *
- # from PIL import Image, ImageDraw
- # import Image, ImageTk
- 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
- number_of_points=len(list_of_points)
- # Draw polygon
- if number_of_points > 2:
- poly=canvas.create_polygon(list_of_points, fill='', outline='green', width=2)
- elif number_of_points == 2 :
- print('line')
- canvas.create_line(list_of_points)
- else:
- print('dot')
- print(list_of_points)
- print
- print
- #
- root = Tk()
- canvas = Canvas(root,height=600,width=600)
- canvas.pack()
- # bind function to canvas to generate event
- canvas.bind("<Button 1>", draw_polygons)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement