views:

1749

answers:

10

Hello there.

I've been learning python for a while now with some success. I even managed to create one or two (simple) programs using PyGtk + Glade.

The thing is: I am not sure if the best way to use GTK with python is by building the interfaces using Glade.

I was wondering if the more experienced ones among us (remember, I'm just a beginner) could point out the benefits and caveats of using Glade as opposed to creating everything in the code itself (assuming that learning the correct gtk bindings wouldn't exactly be a problem).

+10  A: 

Use GtkBuilder instead of Glade, it's integrated into Gtk itself instead of a separate library.

The main benefit of Glade is that it's much, much easier to create the interface. It's a bit more work to connect signal handlers, but I've never felt that matters much.

John Millikin
You can also create files that can be used via GtkBuilder with Glade. Theoretically, that is, since Glade still messes them up.The best option might be to create the GUI with glade, save it as a glade file and then convert with gtk-builder-convert.
Torsten Marek
What version of Glade are you using? The last few versions have the ability to save to either GtkBuilder format of LibGlade format. So you do not need to use gtk-builder-convert.
DoR
+11  A: 

I would say that it depends: if you find that using Glade you can build the apps you want or need to make than that's absolutely fine. If however you actually want to learn how GTK works or you have some non-standard UI requirements you will have to dig into GTK internals (which are not that complicated).

Personally I'm usually about 5 minutes into a rich client when I need some feature or customization that is simply impossible through a designer such as Glade or Stetic. Perhaps it's just me. Nevertheless it is still useful for me to bootstrap window design using a graphical tool.

My recommendation: if making rich clients using GTK is going to be a significant part of your job/hobby then learn GTK as well since you will need to write that code someday.

P.S. I personally find Stetic to be superior to Glade for design work, if a little bit more unstable.

Boris Terzic
Since Glade makes XML files instead of actual python code, is there a good way to start a project with Glade and then hand code more or tweak it? I have just started learning glade personally.
TimothyAWiseman
I think that comment is worth a separate SO question, no?
Boris Terzic
Good point, Boris. I did just that.
TimothyAWiseman
GTK+ internals are not that complicated if you don't go too deep...
ntd
+4  A: 

Glade is very useful for creating interfaces, it means you can easily change the GUI without doing much coding. You'll find that if you want to do anything useful (e.g. build a treeview) you will have to get familiar with various parts of the GTK documentation - in practice finding a good tutorial/examples.

fluteflute
+4  A: 

I started out using glade, but soon moved to just doing everything in code. Glade is nice for simple things, and it's good when you're learning how GTK organizes the widgets (how things are packed, etc). Constructing everything in code, however, you have much more flexibility. Plus, you don't have the glade dependency.

Jeremy Cantrell
+2  A: 

I usually start with Glade until I come to a point where it doesn't have the features I need, e.g. creating a wizard. As long as I'm using the standard widgets that Glade provides, there's really no reason to hand-code the GUI.

The more comfortable I become with how Glade formats the code, the better my hand-coding becomes. Not to mention, it's real easy to use Glade to make the underlying framework so you don't have to worry about all the initializations.

crystalattice
+1  A: 

For quick and simple screens I use Glade. But for anything that needs finer levels of control, I create a custom classes for what I actually need (this is important, because it's too easy to get carried away with generalisations).

With a skinny applications specific classes, I can rapidly change the look and feel application wide from a single place. Rather like using CSS to mantain consistency for web sites.

CyberED
+2  A: 

I recommend using Glade for rapid development, but not for learning. Why? because some times you will need to tune up some widgets in order to work as you want they to work, and if you don't really know/understand the properties attributes of every widget then you will be in troubles.

markuz
+3  A: 

If you're writing a traditional GUI application which reuses a lot of standard components from GTK+ (buttons, labels, containers etc.) I'd personally go with Glade + Kiwi (a convenience framework for building GTK+ GUI applications).

The single greatest advantage to using Glade is that it greatly reduces layout/packing code. Here's an extremely simply example which already shows the issues with manually laying out a GUI (without using any helper functions):

container = gtk.HBox()
label = gtk.Label(str="test")
container.add(label)

For more examples take a look here. Even if you're writing a complicated custom widget you can always create a placeholder in Glade and replace that after instantiation.

It shouldn't be all too long now for the Glade team to release a new version of the designer (3.6.0). This new version will add support for GtkBuilder, which replaces libglade (the actual library that transforms the Glade XML files into a widget tree). The new Glade designer also once again adds support for defining catalogs (sets of widgets) in Python, so you can easily add your own custom widgets.

Bruce van der Kooij
+1  A: 

Personally I would recommend coding it out instead of using Glade. I'm still learning python and pyGtk but I will say that writing out the UI by hand gave me a lot of insight on how things work under the hood.

Once you have it learned I'd say to give glade, or other UI designers a try but definitely learn how to do it the "hard" way first.

A: 

First, start to put this in perspective.

You will be using GTK. This is a huge C library built in 1993 using the best traditions of 1970s coding style. It was built to help implement the GIMP, a Photoshop competitor wanna-be with user interface blunders of legend. A typical gui field might have forty or more parameters, mostly repetitive, having getters and setters. There will be pain.

The GTK itself manages a complete dynamic type system in C using GObject. This makes debugging a special joy that requires manually walking through arrays of pointers to methods full of generic argument lists with implicit inheritance. You will also be jumping through Pango libraries when you least expect it, e.g., using a Pango constant for where in a label the ellipsis go when the page is small. Expect more pain.

By now, you are probably vowing to wrap all your GTK interactions in a Model-View-Controller architecture specific to your application. This is good.

Using Glade, or gtkBuilder, or Stetic, will help coral the huge coupling problem of forty parameters to a function. Glade provides a basic GUI builder to drag and drop components together. The parameters and inherited parameters are somewhat separated out. The output of Glade is .glade XML file which you will then read in, attach your callbacks ("signal handlers") to identically named functions, and query or update the in-memory version of that XML to get widgets that you then use pyGTK to manipulate. Glade itself is a creaky and not well maintained.

Using pyGTK gives you annoyingly fine grained control in order to build your GUI. This will be verbose, copy-and-paste code. Each attribute will be a separate function call. The attribute setter does not return anything, so chaining the calls is out of the question. Usually, your IDE will give only minimal help on what functions mean and you will be constantly referring to DevHelp or some other tool.

One would almost expect GTK GUIs were meant to fail.

Charles Merriam
(Score:-1, Flamebait)
DoR
(Score:-1, Flamebait indeed)
Andy Barlow
Off-topic answer, but I enjoyed it because it's really thorough. I can't confirm its accuracy unfortunately.
Tshepang
Thank you. It's flamebait I suppose, as is any criticism of Gnome.
Charles Merriam