#!/usr/bin/python """ASK Notation Editor""" from Tkinter import * from Canvas import Line, Rectangle, Oval from math import sqrt, atan2, pi, sin, cos, floor from random import randint class Editor(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) Pack.config(self) self.create_widgets() def create_widgets(self): self.master.title("ASK Notation Editor") self.pack(fill=BOTH, expand=Y) self.command = Text(self, height=1) self.command.pack(fill=X, expand=N, side=BOTTOM) self.canvas = Canvas(self, width=800, height=600, background='blue') self.canvas.pack(fill=BOTH, expand=Y, side=TOP) class Node: # id = 0 def __init__(self, name, x=0, y=0, parent=None, size=5, fill='red'): # self.id = Node.id # Node.id = Node.id + 1 self.parent = parent self.name = name self.x = x self.y = y self.children = [] self.fill = fill self.size = size if parent: parent.add_child(self) def show(self, canvas, parent_x=0, parent_y=0):#, ancestors=[]): self.canvas = canvas x = self.x = self.x + parent_x y = self.y = self.y + parent_y r = self.size # ancestors.append(self.id) self.body = Oval(canvas, x-r, y-r, x+r, y+r, fill=self.fill)#, tags=[ancestors]) for child in self.children: child.show(canvas, x, y)#, ancestors) # ancestors.pop() self.body.bind('', self.mouse_down) self.body.bind('', self.mouse_move) self.body.bind('', self.mouse_up) def hide(self): self.canvas = None def add_child(self, child): self.children.append(child) def move_by(self, dx, dy): self.x = self.x + dx self.y = self.y + dy self.body.move(dx, dy) for child in self.children: child.move_by(dx, dy) def move_to(self, x, y): self.move_by(x - self.x, y - self.y) def mouse_down(self, event): Node.lastx = event.x Node.lasty = event.y def mouse_move(self, event): self.move_by(event.x - Node.lastx, event.y - Node.lasty) Node.lastx = event.x Node.lasty = event.y if self.parent: self.parent.constrain(self) def mouse_up(self, event): if self.parent: self.parent.constrain(self) def constrain(self, child): r = self.size dx = child.x - self.x dy = child.y - self.y d = sqrt(dx*dx + dy*dy) dx = dx * r / d dy = dy * r / d # a = atan2(dx, -dy)*180/pi # a = floor(a / 30 + 0.5)*30 # a = a*pi/180 child.move_to(self.x + dx, self.y + dy) # child.move_to(self.x + r*sin(a), self.y - r*cos(a)) editor = Editor() node = Node('Sam', 20, 20, size=20) for i in range(0, 3): Node('Sophie', randint(-14,14), randint(-14,14), node, fill='yellow') Node('Jackie', randint(-14,14), randint(-14,14), node, fill='green') Node('Fred', randint(-14,14), randint(-14,14), node, fill='black') node.show(editor.canvas) editor.mainloop()