Skip to content

Instantly share code, notes, and snippets.

@falsetru
Created February 21, 2012 16:01
Show Gist options
  • Save falsetru/1877152 to your computer and use it in GitHub Desktop.
Save falsetru/1877152 to your computer and use it in GitHub Desktop.
from Tkinter import *
from math import hypot
class CircleDrawing:
def __init__(self, canvas):
self.canvas = canvas
def click(self, e, color):
self.color = color
self.center = e.x, e.y
self.obj_id = self.canvas.create_oval(e.x, e.y, e.x, e.y, outline=color)
def drag(self, e):
x1, y1 = self.center
self.radius = distance = int(hypot(e.x-x1, e.y-y1))
self.canvas.delete(self.obj_id)
self.obj_id = self.canvas.create_oval(x1-distance, y1-distance, x1+distance, y1+distance, outline=self.color)
def __str__(self):
return "(draw-circle (make-posn {0[0]} {0[1]}) {1} '{2})".format(
self.center, self.radius, self.color)
class LineDrawing:
def __init__(self, canvas):
self.canvas = canvas
def click(self, e, color):
self.color = color
self.start = e.x, e.y
self.obj_id = self.canvas.create_line(e.x, e.y, e.x, e.y, fill=self.color)
def drag(self, e):
x1, y1 = self.start
self.end = e.x, e.y
self.canvas.delete(self.obj_id)
self.obj_id = self.canvas.create_line(x1, y1, e.x, e.y, fill=self.color)
def __str__(self):
return "(draw-solid-line (make-posn {0[0]} {0[1]}) (make-posn {1[0]} {1[1]}) '{2})".format(
self.start, self.end, self.color)
class ShapeDump(Frame):
def __init__(self, parent):
self.objs = []
Frame.__init__(self, parent)
self.canvas = Canvas(self)
self.canvas.pack(expand=1, fill=BOTH)
self.canvas.bind('<Button-1>', self.click)
self.canvas.bind('<ButtonRelease-1>', self.done)
self.canvas.bind('<B1-Motion>', self.drag)
frame = Frame(self)
frame.pack(fill=BOTH)
Button(frame, text='circle', command=self.set_circle_mode).pack(side=LEFT, expand=1, fill=BOTH)
Button(frame, text='line', command=self.set_line_mode).pack(side=LEFT, expand=1, fill=BOTH)
Button(frame, text='POP', command=self.pop).pack(side=LEFT, expand=1, fill=BOTH)
Button(frame, text='DUMP', command=self.dump).pack(side=LEFT, expand=1, fill=BOTH)
Button(frame, text='CLEAR', command=self.clear).pack(side=LEFT, expand=1, fill=BOTH)
frame = Frame(self)
frame.pack(fill=BOTH)
for color in ('red', 'black', 'blue'):
Button(frame, text="'"+color, command=lambda c=color: self.set_color(c)).pack(
side=LEFT, expand=1, fill=BOTH)
self.set_circle_mode()
self.set_color('black')
def set_color(self, color):
self.color = color
def set_circle_mode(self):
self.drawing_cls = CircleDrawing
def set_line_mode(self):
self.drawing_cls = LineDrawing
def click(self, e):
self.drawing = self.drawing_cls(self.canvas)
self.drawing.click(e, self.color)
def drag(self, e):
self.drawing.drag(e)
def done(self, e):
self.objs.append(self.drawing)
def pop(self):
obj = self.objs.pop()
self.canvas.delete(obj.obj_id)
def dump(self):
for obj in self.objs:
print obj
def clear(self):
del self.objs[:]
self.canvas.delete(ALL)
root = Tk()
ShapeDump(root).pack(expand=1, fill=BOTH)
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment