[pygtk] memory, del, and classes that has gtk.Window inside...

Alessandro Dentella sandro at e-den.it
Fri May 21 23:57:00 WST 2010


Hi,

I'm trying to figure out the correct way to delete a class that builds
GUI. 

I was not able to delete the object from a callback in a clean way.

The following code should demostrate what I mean::

  import weakref
  import gtk
  import gobject

  class A(object): pass


  class G(object):
      def __init__(self):
	  self.a = A()
	  self.w = gtk.Window()
	  self.w.show()
	  self.w.connect_after('delete-event', self.delete_event_cb)

      def delete_event_cb(self, win, ev):
	  print 'killed'
	  del self
	  print "s", s()   # I'd like to see this to become None
	  gtk.main_quit()

  g = G()

  s = weakref.ref(g.a)

  gtk.main()

My goal is to create a class 'G'  that will destroy itself completely
when the window it holds receives a "delete_event". 

As written in the comment, I'd like to see that the weakref to an element
inside the instance 'g' should be None after deletion of 'g'. But it's not!

Deleteing the object prints:

  sandro a bluff: $ python test_mem.py
  killed
  s <__main__.A object at 0x8329eec>

If I get rid of 'self'in the callback, everithing works but that makes
it impossible to put the function inside the class.

If I use a delete_event_cb as a separate function I manage to destroy it if
I don't pass the g object as argument to the callbac (ie: making it global)
but clearly that's a far less usefull setup.

What's the best practice to delete compound widgets?


sandro
*:-)



-- 
Sandro Dentella  *:-)
http://sqlkit.argolinux.org        SQLkit home page - PyGTK/python/sqlalchemy


More information about the pygtk mailing list