Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # polygon_edit.py
- from Tkinter import *
- #Tk()
- import sys
- root = Tk()
- R = 10
- p = []
- history = []
- def f_redraw():
- w.delete(ALL)
- for i in range(len(p)):
- j = (i + 1) % len(p)
- w.create_line(p[i][0], p[i][1], p[j][0], p[j][1], fill="red")
- # enumerate([x, y, z]) = [(0, x), (1, y), (2, z)]
- for i, (x, y) in enumerate(p):
- w.create_oval(x - R, y - R, x + R, y + R,
- fill="green", outline="#00aa00")
- w.create_text(x, y, fill="white",
- text=str(i+1), justify=CENTER, font="Courier 10 bold")
- def f_key(event):
- c = repr(event.char)
- print("pressed key {0} {1}".format(c, event.char))
- if event.char == 'q':
- sys.exit(0)
- def dist(a, b):
- return (a[0]-b[0])**2 + (a[1]-b[1])**2
- def f_delete(event):
- print("clicked-2 at", event.x, event.y)
- global p
- if len(p) == 0:
- return
- pos = (event.x, event.y)
- i = 0
- for j in range(len(p)):
- if dist(pos, p[j]) < dist(pos, p[i]):
- i = j
- history.append(list(p))
- p = p[:i] + p[i+1:]
- f_redraw()
- def f_cancel(event):
- global p, history
- print("cancel: len(history) = %d" % len(history))
- if len(history) > 0:
- p = history[-1]
- history = history[:-1]
- f_redraw()
- def f_button(event):
- print("clicked-1 at", event.x, event.y)
- global R, history, p
- w.focus_set()
- history.append(list(p))
- p.append((event.x, event.y))
- f_redraw()
- w = Canvas(root, width=400, height=400)
- w.focus_set()
- w.bind("<Key>", f_key)
- w.bind("<Control-z>", f_cancel)
- w.bind("<Control-Z>", f_cancel)
- w.bind("<Button-1>", f_button)
- w.bind("<Button-3>", f_delete)
- w.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement