On Thu, 2002-04-18 at 12:14, Stephen Langer wrote:
> 
> I would be very interested in seeing the demo.  As I said, we haven't
> gotten to the point of moving lines yet, but we will.  Anything to
> save a few mistakes would be welcome.
> 
> Thanks. 
> 
>  -- Steve

Ok, here is an example derived from a larger program I'm working on. I
originally started with the pygnome Canvas example, and figured out how
to add lines.

This demo puts an ellipse in the middle of the screen, which reacts to
you mousing over it. You can also drag out an arrow from the ellipse,
and if you then mouse over the ellipse, it will also draw the arrow.

There is a bug here somewhere, though, and I'm pretty confused about it.
I'm using pretty much the same code in a larger program, which seems to
work fine. But this small example gets hung up in the widget.set() call
on line 109, where it sets the colour to cyan and the line width to 3.
For some reason, python seems to go into an infinite loop occasionally,
if you move your mouse over the object repeatedly.

Is this a library problem, or a bug in my code? I'm using python 2.2,
and gnome-python-1.0.53, pygtk-0.6.6.

Hope this is helpful.

-- 
Colin Fox                                       [EMAIL PROTECTED]
CF Consulting Inc.                                    GPG Fingerprint:
                    D8F0 84E7 E7CC 5C6C 9982  F1A7 A3EB 6EA3 BC97 572F
#!/usr/bin/env python

import GDK, gtk, gnome.ui

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 == GDK.BUTTON_RELEASE:
	    if event.button == 1:
		self.lmb_pressed = 0
		self.myline.hide()
		return gtk.TRUE

        elif event.type == 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 == GDK.MOTION_NOTIFY:
            if event.state & GDK.BUTTON1_MASK:
		self.set_line_endpoint(event.x, event.y)
		return gtk.TRUE
 
        elif event.type == 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 == 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('ellipse',
	    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('line',
	    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.GtkWindow()
	win.connect('destroy', gtk.mainquit)
	win.set_title('Simple Example')

	self.canvas = gnome.ui.GnomeCanvas()
	self.canvas.set_usize(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()

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to