Hi, everyone.

I was using gnome-python pre2.0 for some time, with great success.
Unfortunately things seem to have changed for gnome-python 2.0 and now
my programs don't run.

I've already done the various conversions needed to support modules
moving around and being renamed. What I'm now stuck on is trying to add
a line to a gnome canvas object. I get an incomprehensible error, and
I've no way to determine what's wrong.

I've attached the program. The problem is in the call to 'build' within
the WorldObject. The first add() statement dies like this:

 
(tinydemo.py:5601): GnomeCanvas-CRITICAL **: file gnome-canvas.c: line
198 (gnome_canvas_item_new): assertion `g_type_is_a (type,
gnome_canvas_item_get_type ())' failed
Traceback (most recent call last):
  File "./tinydemo.py", line 106, in ?
    st.populate()
  File "./tinydemo.py", line 89, in populate
    self.element = WorldObject(self.canvas,200,200)
  File "./tinydemo.py", line 16, in __init__
    self.build()
  File "./tinydemo.py", line 67, in build
    fill_color='darkgrey')
RuntimeError: could not create canvas item object

Can anyone either shed some light, or point me to some documentation to
show how to add canvas objects in 2.0? I originally wrote this based on
the gnome-python example, but I can't find a 2.0 equivalent of that
example.

I've attached my program for reference.

Thanks in advance!

cf
#!/usr/bin/env python

import gtk, gtk.gdk, gnome.ui, 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[0] = x
	self.mylinepoints[1] = 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.myline = self.canvas.root().add('line',
	    points=self.mylinepoints,
	    width_units=1.0,
	    line_style=gtk.gdk.LINE_ON_OFF_DASH,
	    arrow_shape_a=10.0,
	    arrow_shape_b=15.0,
	    arrow_shape_c=5.0,
	    first_arrowhead=gtk.TRUE,
	    last_arrowhead=gtk.FALSE,
	    fill_color='darkgrey')

	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.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(gtk.WINDOW_TOPLEVEL)
	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