5

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?

2 Answers 2

7

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.

2

Keep in mind that you need Glade 3.10 or newer to build a GTK 3 interface. 3.8 or older is for GTK 2. In Ubuntu, the packages are called glade and glade-gtk2.

If you need to convert an (old) PyGTK app to PyGI, you should try the pygi-convert script that comes with the PyGObject tarball. In a lot of cases, this should be enough to transform your application. It is also found in GIT: http://git.gnome.org/browse/pygobject/tree/pygi-convert.sh

The following link should help too with some basic explanation of the common things that have changed: https://live.gnome.org/PyGObject/IntrospectionPorting

And finally, here is an (very nice) effort to create a PyGI tutorial: http://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/index.html

I find it usefull to have a look at the C docs when building something: http://developer.gnome.org/gtk3/ Once you know how everything is done in Python, this can be a very good resource for things that aren't documented yet. Let's have a look at the gtk.RESPONSE_OK example given in the other answer. When going to the C docs, you'll find this: http://developer.gnome.org/gtk3/3.2/GtkDialog.html#GtkResponseType Take the enum name (GtkResponseType) and split the 'Gtk' part by a dot and add the enum type without the GTK_* part, so you'll get: Gtk.ResponseType.OK This works with all enums I came across.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .