Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the steps to convert from using libglade to GtkBuilder? (Python)

I have a small project that uses libglade and use the following to load the xml file:

self.gladefile = "sdm.glade"
self.wTree = gtk.glade.XML(self.gladefile) 
self.window = self.wTree.get_widget("MainWindow")
if (self.window):
    self.window.connect("destroy", gtk.main_quit)
dic = { "on_button1_clicked" : self.button1_clicked, 
        "on_MainWindow_destroy" : gtk.main_quit}
self.wTree.signal_autoconnect(dic)

After converting my project in glade, what structural changes do I need to make? I'm on Ubuntu 9.04.

like image 669
lfaraone Avatar asked Apr 14 '09 18:04

lfaraone


2 Answers

You need to use gtk.Builder instead. This class can load any number of UI files, so you need to add them manually, either as files or as strings:

self.uifile = "sdm.ui"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.uifile)

Instead of get_widget, just use get_object on the builder class:

self.window = self.wTree.get_object("MainWindow")
if self.window:
    self.window.connect("destroy", gtk.main_quit)

To connect the signals, just use connect_signals, which also takes a dictionary:

dic = { "on_button1_clicked" : self.button1_clicked, 
    "on_MainWindow_destroy" : gtk.main_quit}
self.wTree.connect_signals(dic)

It used to be the case (at least in GTK+ 2.12, not sure if it's still the same) that you could call connect_signals only once, any signals which are not connected during the first invocation will never be connected. This was different in glade, so be careful if you relied on that feature before.

like image 109
Torsten Marek Avatar answered Sep 20 '22 16:09

Torsten Marek


Torsten's answer is correct, but a little incomplete, so in the spirit of http://xkcd.com/979/ here is the procedure I recently settled on after much trial-and-error:

Open yada.glade in Glade interface designer. Go to edit->project and change the project type to GtkBuilder and make sure it targets the latest version (2.24 as of this writing). Save the file, being sure that it saves in GtkBuilder format, and change the name from yada.glade to yada.ui

Open yada.py and change the following code:

gladefile = relativize_filename(os.path.join("glade", "yada.glade"))
self.wTree = gtk.glade.XML(gladefile, self.windowname)

to:

uifile = relativize_filename(os.path.join("glade", "yada.ui"))
self.wTree = gtk.Builder()
self.wTree.add_from_file(uifile)

Similarly change all instances of self.wTree.get_widget(...) to self.wTree.get_object(...)

Change self.wTree.signal_autoconnect(dic) to self.wTree.connect_signals(dic)

If your code depends on the name assigned the widget in the interface designer, change widget.get_name() to gtk.Buildable.get_name(widget). widget.get_name() now just returns the widget type. EDIT: You also need to change widget.set_name('my_widget') to gtk.Buildable.set_name(widget, 'my_widget').

Delete import gtk.glade

I found numerous unused signals defined in the yada.ui xml file, I had to open the xml file and manually delete them to eliminate the warnings they caused.

like image 38
rcriii Avatar answered Sep 18 '22 16:09

rcriii