Libglade to GtkBuilder F.A.Q.

I try to help new users with GTK+ programming and Glade as often as I have time in the mailing lists, forums, and emails in response from my tutorials. I posted this to answer some of the more frequent questions I get about GtkBuilder and Libglade.

Libglade and GtkBuilder are libraries which are used by programmers to dynamically parse an XML file describing a GUI (usually created with Glade Interface Designer) and build the GUI. Using Glade with Libglade or GtkBuilder allows for RAD (Rapid Application Development) of GTK+ applications in a multitude if programming languages such as C, C++, PHP, Python, etc. If you don't know this already, this FAQ probably will not be very relevant to you and you should instead start with: GTK+ and Glade3 GUI Programming Tutorial - Part 1

This is put together based on my conversations in mailing lists, bug reports, and experimentation and is not an "official" FAQ as part of GTK+ or Glade as I am a GTK+/Glade user and do not work on either. I also may express personal opinions in the answers to these questions.

  1. Why is generating code with the 'Build' feature in Glade 2 "bad"?
  2. Which should I use in my application: Libglade or GtkBuilder?
  3. Why Is Libglade being replaced by GtkBuilder... what's wrong with Libglade?
  4. Why do I have to use gtk-builder-convert? Why can't I just save in GtkBuilder format directly from Glade?
  5. Why does gtk-builder-convert crash saying "UnboundLocalError: local variable 'child' referenced before assignment"?
  6. Why does gtk-builder-convert crash saying "AttributeError: Document instance has no attribute 'tagName'"
  7. Where are you getting this information from?

Why is generating code with the 'Build' feature in Glade 2 "bad"? 

Although this may be an opinion, it is an opinion that is shared among a majority of developers working on real-world, large-scale GTK+ applications. It is so highly recommended to avoid this deprecated feature of the older Glade 2 that is was completely removed in Glade 3.

There are many arguable reasons why it's "bad" to generate C code from Glade 2 using the 'Build' feature. Here are a few...

  • No choice in programming language. Using the Libglade/GtkBuilder methods can be implemented in dozens of languages independent of the GUI designed in Glade.
  • The style and naming conventions are not likely to match those of the application programmer's or the community at large.
  • The generated code will likely uses deprecated code and naming conventions.
  • Most experienced developers won't want to help with problems that may come up as they would likely be avoided by avoiding code generation.
  • Generating the code from glade again after editing previously generated code is likely to have problems.

Which should I use in my application: Libglade or GtkBuilder? 

You should use GtkBuilder if you can as GtkBuilder is expected to replace Libglade (Why?) and GtkBuilder is built right into GTK+ thus reducing your application's dependencies. However, you (and your end users) will need at least GTK+ 2.12 and you will have to run the files you create with Glade through a conversion script until Glade 3 supports saving in the GtkBuilder format directly (When?).

The vast majority of tutorials and large projects out there will still be using Libglade. Being that Libglade and GtkBuilder are so very similar in terms of the API, you shouldn't have trouble using one or the other depending on what you're working on. You don't need to panic about using one or the other, just be aware that GtkBuilder is the future. If you have an application already using Libglade, you could switch now or wait until it's better supported. However, I would recommend using GtkBuilder in new applications to avoid having to migrate the code later.

Why Is Libglade being replaced by GtkBuilder... what's wrong with Libglade? 

Using Libglade to dynamically load UI descriptions in GTK+ applications is so commonplace, that it doesn't make sense NOT to have it be part of the GTK+ library. GtkBuilder does just that and then adds some functionality. GtkBuilder supports GtkTreeView and related widgets including models, GtkMenu and GtkToolbar widgets using GtkUIManager and much more as it evolves. A lot of work was put into it by a lot of people including GTK+ and Glade developers.

Essentially, nothing is really wrong with Libglade. GtkBuilder simply re-thinks the internal workings to make it more versatile and adhere to standards and package it along with the rest of the GTK+ library. If you want to know more about how this all evolved, you can read Johan Dahlin's blog post: GtkBuilder Has Landed and the bug tracker: Add support for UI builders in gtk+.

Why do I have to use gtk-builder-convert? Why can't I just save in GtkBuilder format directly from Glade? 

As GtkBuilder is relatively new, Glade does not yet support saving directly in the GtkBuilder XML format (although the 3.5 development series partially does). GTK+ includes a script which will convert the Glade XML format (.glade) to the newer GtkBuilder format called gtk-builder-convert.

The Glade developers are working hard on getting GtkBuilder support integrated into Glade and are aiming to have gtk-builder-convert obsolete by Glade 3.6. You can follow along with bug #490678 to keep up on the status of GtkBuilder support in Glade

I personally feel (and many agree) that it is better to deal with the inconvenience of an extra step running gtk-builder-convert for the short term than to promote using Libglade.

Why does gtk-builder-convert crash saying "UnboundLocalError: local variable 'child' referenced before assignment"? 

This is a bug (#523932) in which Glade puts a custom property called "use_stock" in GtkImageMenuItems to indicate a stock menu item but does not put any children (such as the child image). This is the default way in which menus are generated in Glade and was not supported by gtk-builder-convert. The bug has been fixed and should not be a problem in the next versions of GTK+.

As a workaround for this bug if you have an older version of GTK+, you can see the section titled "Editing the Menu (or Toolbar)" in GTK+ and Glade3 GUI Programming Tutorial - Part 1.

Why does gtk-builder-convert crash saying "AttributeError: Document instance has no attribute 'tagName'" 

This is a bug (#496344) in which Glade puts a response_id attribute in with GtkButton widgets even though that property is irrelevant unless it's in a GtkDialog widget's action area. This was fixed, (I can't recall which version) in the gtk-builder-convert script. I know that GTK+ 2.12.9 does not have this problem, but I believe it was fixed prior.

If you cannot upgrade your version of GTK+, you can manually delete the lines that look like something like this in your .glade file (these will be within the <child> tags of a GtkButton) prior to running gtk-builder-convert:

<property name="response_id">0</property>

Where are you getting this information from? 

It's important to know where to look if you have a problem using Glade or GtkBuilder. You may have stumbled upon a bug that the developers are not aware of. Many people struggle with issues that have already been addressed, but don't know where to look.

  • GtkBuilder bug list The bugzilla bug list for the GtkBuilder portion of GTK+ contains bugs related to using GtkBuilder and the gtk-builder-convert script.
  • Glade3 bug list The bugzilla bug list for Glade3 contains the bugs related to using Glade3.
  • The glade-users mailing list The developers and users of glade are very helpful on this mailing list. If you have a question related to using Glade, this is a good place to ask.
  • The gtk-app-devel mailing list The gtk-app-devel mailing list is a good place to ask for help with using GtkBuilder or gtk-builder-convert.
Did you enjoy Libglade to GtkBuilder F.A.Q.? If you would like to help support my work, A donation of a buck or two would be very much appreciated.
blog comments powered by Disqus
Linux Servers on the Cloud IN MINUTES