Ubuntu – Can glade be used with python pygi introspection

application-developmentgtkpygtkpython

I once got started on a PyGtk and Glade tutorial but never finished it. Recently I saw that PyGtk is being depreciated in favor of using introspection at https://wiki.ubuntu.com/MeetingLogs/appdevweek1104/GObjectIntrospection and https://wiki.ubuntu.com/MeetingLogs/appdevweek1104/PyGI. I decided to try learning more of python and Gtk again using the new way. The pages I linked to seem to show how you would do it if I wanted to make the GUI in the code, but I like to use Glade. Is there a way to do this and still use the new introspection?

Best Answer

Yes, in fact it's almost exactly the same:

from gi.repository import Gtk

class Test (object):

    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("test.glade")
        self.builder.connect_signals(self)

    def run(self, *args):
        self.builder.get_object("window1").show()
        Gtk.main()

    def quit(self, *args):
        Gtk.main_quit()


Test().run()

Many smaller programs will take little effort to convert. You can start by switching to gobject introspection using the following two lines, and then correct any errors by looking them up in the reference.

from gi.repository import Gtk as gtk
from gi.repository import Gobject as gobject

For example, gtk.RESPONSE_OK will be called Gtk.ResponseType.OK when you're using introspection.