[pygtk] Can't run PyGTK more than once from embedded Python

Patrick K. O'Brien pobrien at orbtech.com
Sun Jan 8 05:24:19 WST 2006


Romain Behar wrote:
> Running the following script from the command line
> opens a window, but after closing it, one needs to hit
> Ctrl-C to stop the Python interpreter:
> 
> 
> import pygtk
> pygtk.require('2.0')
> import gtk
> 
> class Base:
> 	def __init__(self):
> 		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> 		self.window.show()
> 
> 	def main(self):
> 		gtk.main()
> 
> print __name__
> if __name__ == "__main__":
> 	base = Base()
> 	base.main()
> 
> 
> Is it possible for the above script to quit when the
> window's closed?

Yes.  Something needs to call gtk.main_quit() to stop the GTK event
loop.  All of this is spelled out pretty well in the tutorial:

http://www.pygtk.org/pygtk2tutorial/ch-GettingStarted.html

Short answer - add this to the Base.__init__():

        self.window.connect('destroy', gtk.main_quit)

When the window is destroyed (ie, closed) then gtk.main_quit will get
called.  Here is the complete code:

import pygtk
pygtk.require('2.0')
import gtk

class Base:
    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect('destroy', gtk.main_quit)
        self.window.show()

    def main(self):
        gtk.main()

print __name__
if __name__ == "__main__":
    base = Base()
    base.main()

-- 
Patrick K. O'Brien
Orbtech       http://www.orbtech.com
Schevo        http://www.schevo.org
Louie         http://louie.berlios.de



More information about the pygtk mailing list