Glade or no glade: What is the best way to use PyGtk?
Asked Answered
S

11

33

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).

Spadework answered 7/9, 2008 at 4:0 Comment(2)
There are more UI builders than just glade, maybe you should mention "UI builder" instead of just glade.Ardellaardelle
Using a graphical tool that builds GtkBuilder UI XML is certainly standard for most gtk programs. It doesn't save you from working with the gtk api yourself, but it certainly makes it easier for other people to pick up your code.Aggri
A
20

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.

Anthotaxy answered 7/9, 2008 at 20:9 Comment(2)
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.Venetian
I think that comment is worth a separate SO question, no?Anthotaxy
G
12

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.

Gur answered 7/9, 2008 at 4:20 Comment(3)
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.Kiger
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.Feldt
In fact, gtk-builder-convert will choke on the GtkBuilder format produced by glade-3, so that you know you are already compatable.Nicely
P
5

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.

Ppi answered 10/9, 2008 at 15:4 Comment(0)
L
5

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.

Linked answered 20/9, 2008 at 3:4 Comment(0)
W
4

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.

Williamswilliamsburg answered 20/9, 2008 at 11:40 Comment(0)
M
4

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.

Metempsychosis answered 7/2, 2009 at 21:19 Comment(0)
V
3

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.

Vote answered 27/1, 2010 at 17:45 Comment(2)
Off-topic answer, but I enjoyed it because it's really thorough. I can't confirm its accuracy unfortunately.Forceful
Thank you. It's flamebait I suppose, as is any criticism of Gnome.Vote
S
2

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.

Simulacrum answered 20/11, 2008 at 15:33 Comment(0)
M
1

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.

Monosaccharide answered 13/10, 2008 at 22:2 Comment(0)
L
1

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.

Latinity answered 15/4, 2009 at 13:32 Comment(0)
P
-1

You may use glade-2 to design, and use glade2py.py to generating the pure pygtk code, it use pygtkcompat to support gtk3

Percent answered 27/6, 2014 at 10:9 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Plimsoll
@Plimsoll this code is to long, can not paste in to there.Percent

© 2022 - 2024 — McMap. All rights reserved.