Created
February 28, 2012 16:05
-
-
Save falsetru/1933365 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-rotated-posn {0[0]} {0[1]} degree) {1} '{2})".format( | |
self.center, self.radius, self.color) | |
class SolidDiskDrawing: | |
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, fill=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, fill=self.color) | |
def __str__(self): | |
return "(draw-solid-disk (make-rotated-posn {0[0]} {0[1]} degree) {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-rotated-posn {0[0]} {0[1]} degree) (make-rotated-posn {1[0]} {1[1]} degree) '{2})".format( | |
self.start, self.end, self.color) | |
class CanvasItemDrag: | |
def __init__(self, canvas): | |
self.canvas = canvas | |
self.canvas.bind('<Button-2>', self.initiate) | |
self.canvas.bind('<B2-Motion>', self.move) | |
def initiate(self, e): | |
self.moving_obj_id = self.canvas.find_closest(e.x, e.y) | |
self.moving_pos = e.x, e.y | |
def move(self, e): | |
x, y = self.moving_pos | |
self.canvas.move(self.moving_obj_id, e.x-x, e.y-y) | |
self.moving_pos = e.x, e.y | |
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) | |
CanvasItemDrag(self.canvas) | |
frame = Frame(self) | |
frame.pack(fill=BOTH) | |
self.mode_buttons = [] | |
b = Button(frame, text='line', command=self.set_line_mode); self.mode_buttons.append(b); b.pack(side=LEFT, expand=1, fill=BOTH) | |
b = Button(frame, text='circle', command=self.set_circle_mode); self.mode_buttons.append(b); b.pack(side=LEFT, expand=1, fill=BOTH) | |
b = Button(frame, text='disk', command=self.set_solid_disk_mode); self.mode_buttons.append(b); b.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.default_background = b['background'] | |
self.default_activebackground = b['activebackground'] | |
self.set_line_mode() | |
self.set_color('black') | |
def set_color(self, color): | |
self.color = color | |
def set_mode(self, mode, cls): | |
for btn in self.mode_buttons: | |
if btn['text'] == mode: | |
self.drawing_cls = cls | |
btn['activebackground'] = 'blue' | |
btn['background'] = 'blue' | |
else: | |
btn['activebackground'] = self.default_activebackground | |
btn['background'] = self.default_background | |
def set_circle_mode(self): | |
self.set_mode('circle', CircleDrawing) | |
def set_solid_disk_mode(self): | |
self.set_mode('disk', SolidDiskDrawing) | |
def set_line_mode(self): | |
self.set_mode('line', 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