In looking through the archive for topics related to using the gnome canvas, I 
came accross an example submitted by Colin Fox back in Apr 2002. I updated 
the syntax of the example to work under the more recent version of pygtk and 
have appended it below. The example runs, but it doesn't seem to run 
correctly. The canvas starts out responding to enter_notify and leave_notify 
events, but then freezes after a few such events. The canvas also freezes if 
the mouse is clicked over the object. Is this caused by a bug in the example 
or is there a bug in the canvas?
--Doug Blanding

#!/usr/bin/env python

import gtk
import gnome.canvas

class WorldObject:
    def __init__(self,canvas,x,y):
        self.myobj = None
        self.myline = None
        self.mylinepoints = [x,y,x,y]
        self.canvas = canvas
        self.width = 20
        self.height = 20
        self.x = x
        self.y = y
        self.lmb_pressed = 0
        self.build()

    def set_line_endpoint(self,x,y):
        self.mylinepoints[2] = x
        self.mylinepoints[3] = y
        self.myline.set(points=self.mylinepoints)

    def item_event(self, widget, event=None):
        if event.type == gtk.gdk.BUTTON_RELEASE:
            if event.button == 1:
                self.lmb_pressed = 0
                self.myline.hide()
                return gtk.TRUE

        elif event.type == gtk.gdk.BUTTON_PRESS:
            if event.button == 1:
                self.lmb_pressed = 1
                self.set_line_endpoint(event.x,event.y)
                self.myline.show()
                return gtk.TRUE

        elif event.type == gtk.gdk.MOTION_NOTIFY:
            if event.state & gtk.gdk.BUTTON1_MASK:
                self.set_line_endpoint(event.x, event.y)
                return gtk.TRUE
 
        elif event.type == gtk.gdk.ENTER_NOTIFY:
            # Make the outline wide, show the arrow
            widget.set(width_units=3, fill_color="cyan")
            self.myline.show()
            return gtk.TRUE

        elif event.type == gtk.gdk.LEAVE_NOTIFY:
            # Make the outline thin, hide the arrow
            widget.set(width_units=1, fill_color="blue")
            if self.lmb_pressed == 0:
                self.myline.hide()
            return gtk.TRUE

        return gtk.FALSE


    def build(self):
        self.myobj = self.canvas.root().add('GnomeCanvasEllipse',
            x1=self.x-(self.width/2),
            y1=self.y-(self.height/2),
            x2=self.x+(self.width/2),
            y2=self.y+(self.height/2),
            fill_color='blue', outline_color='black',
            width_units=1.0)

        self.myline = self.canvas.root().add('GnomeCanvasLine',
            points=self.mylinepoints,
            width_units=1.0,
            line_style='GDK_LINE_ON_OFF_DASH',
            arrow_shape_a=10.0,
            arrow_shape_b=15.0,
            arrow_shape_c=5.0,
            first_arrowhead=gtk.FALSE,
            last_arrowhead=gtk.TRUE,
            fill_color='black')

        self.myline.hide()
        self.myobj.show()
        self.myobj.connect("event",self.item_event)


class SimpleTest:
    def __init__(self):
        self.width = 400
        self.height = 400
        self.canvas = None

    def populate(self):
        self.element = WorldObject(self.canvas,200,200)

    def open_window(self):
        win = gtk.Window()
        win.connect('destroy', gtk.mainquit)
        win.set_title('Simple Example')

        self.canvas = gnome.canvas.Canvas()
        self.canvas.set_size_request(self.width, self.height)
        self.canvas.set_scroll_region(0,0, self.width, self.height)
        win.add(self.canvas)
        self.canvas.show()
        win.show()

if __name__ == '__main__':
    st = SimpleTest()
    st.open_window()
    st.populate()
    gtk.mainloop()

_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to