Skip to content

Instantly share code, notes, and snippets.

@falsetru
Created September 18, 2011 06:17
Show Gist options
  • Save falsetru/1224800 to your computer and use it in GitHub Desktop.
Save falsetru/1224800 to your computer and use it in GitHub Desktop.
from Tkinter import *
class LineFactory:
def __init__(self):
self.line = None
def text(self, p):
canvas.create_text(p.x, p.y-10,
text='{0.x}, {0.y}'.format(p))
def arrow(self, e):
return canvas.create_line(self.start.x, self.start.y,
e.x, e.y,
arrow=LAST, width=2)
def start(self, e): self.start = e
def move(self, e): canvas.delete(self.line); self.line = self.arrow(e)
def done(self, e):
if self.line:
canvas.delete(self.line)
self.line = None
self.arrow(e)
self.text(self.start)
self.text(e)
print '{0.x},{0.y},{1.x},{1.y}'.format(self.start, e)
class Mover:
def __init__(self):
self.shape = None
def choose(self, e):
self.start = e
x = canvas.canvasx(e.x)
y = canvas.canvasx(e.y)
self.shape = canvas.find_closest(x, y)
def move(self, e):
canvas.move(self.shape, e.x-self.start.x, e.y-self.start.y)
self.start = e
#def save(e):
# fn = '1.eps'
# canvas.postscript(fn)
# Image.open(fn).save('1.png')
# canvas.clear()
l = LineFactory()
m = Mover()
root = Tk()
root.title('')
canvas = Canvas(root, bg='white')
canvas.pack()
canvas.bind('<Button-1>', l.start)
canvas.bind('<B1-Motion>', l.move)
canvas.bind('<ButtonRelease-1>', l.done)
canvas.bind('<Button-2>', m.choose)
canvas.bind('<B2-Motion>', m.move)
#canvas.bind('<Button-3>', save)
canvas.bind('<Button-3>', lambda e: canvas.delete(ALL))
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment