Skip to content
Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env python
# - coding: utf-8 -
# Copyright (C) 2010 Toms Bauģis <toms.baugis at>
"""an example how to connect points by dragging instead of clicking"""
import gtk
from lib import graphics
class Node(graphics.Sprite):
def __init__(self, x, y):
graphics.Sprite.__init__(self, x, y, interactive = True)"#999"),-5, 10, 10, 3)
class Scene(graphics.Scene):
def __init__(self):
self.connect("on-click", self.on_click)
self.connect("on-mouse-down", self.on_mouse_down)
self.connect("on-mouse-up", self.on_mouse_up)
self.connect("on-enter-frame", self.on_enter_frame)
self.connections = []
self.arc_start = None
def on_click(self, scene, event, sprite):
if not sprite:
self.add_child(Node(event.x, event.y))
def on_mouse_down(self, scene, event):
self.arc_start = self.arc_start or self.sprite_at_position(event.x, event.y)
def on_mouse_up(self, scene):
x, y, mods = self.get_pointer()
sprite = self.sprite_at_position(x, y)
if self.arc_start and self.arc_start != sprite:
self.connections.append((self.arc_start, sprite))
self.arc_start = None
def on_enter_frame(self, scene, context):
g = graphics.Graphics(context)
for start, end in self.connections:
g.move_to(start.x, start.y)
g.line_to(end.x, end.y)
class BasicWindow:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(600, 500)
window.connect("delete_event", lambda *args: gtk.main_quit())
example = BasicWindow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.