GTK+ and Glade3 GUI Programming Tutorial - Part 1
Designing a User Interface using Glade3
In part 1 of the GTK+ and Glade3 GUI Programming Tutorial series, we will be designing the graphical user interface (GUI) for a GTK+ text editor application (shown left) which will be used throughout these tutorials. This GUI design will be created using the Glade Interface Designer and is completely independent of the programming language used to implement the design, which will come in subsequent tutorials.
GTK+ and Glade3 GUI Programming Tutorial Contents
-
Part 1 - Designing a User Interface Using Glade 3
- Quick Overview of GTK+ Concepts
- Introduction to Glade3
- Getting Familiar with the Glade Interface
- Manipulating Widget Properties
- Specifying Callback Functions for Signals
- Adding Widgets to the GtkWindow
- How Packing Effects the Layout
- Editing the Menu (or Toolbar)
- Final Touches to the Main Window
- Getting Additional Help Using Glade
- What Next?
- Part 2 - Choosing a Programming Language for GTK+ Development
- Part 3 - Writing a Basic Program to Implement the Glade File
Quick Overview of GTK+ Concepts
If you have no experience with GTK+, you may struggle with some of the concepts I am going to cover. Although I am going to attempt to teach some of these concepts on the fly, it would serve you well to read up on these ideas further, perhaps after working through part 1 of this tutorial. Understanding the fundamental concepts of GTK+ will be instrumental in your ability to effectively use Glade.
First of all, GTK+ is not a programming language. GTK+ is a toolkit, or a collection of libraries, which developers can use to develop GUI applications for Linux, OSX, Windows, and any other platform on which GTK+ is available. It can be thought of in the same terms as MFC or the Win32 API on Windows, Swing and SWT in Java, or Qt (the "other" Linux GUI toolkit used by KDE).
Although GTK+ itself is written in C, there are a multitude of language "bindings" allowing programmers to develop GTK+ applications in the language of their choice including C++, Python, Perl, PHP, Ruby, and many others.
GTK+ is based on 3 main libraries: Glib, Pango, and ATK, however, we primarily work with GTK+ and let GTK+ do it's magic with those 3 libraries. GLib wraps most of the standard C library functions for portability (allowing your code to run on Windows and Linux if desired). We use GLib a lot when working in C or C++, which I will explain more thoroughly when implementing our design using C. Higher-level languages such as Python and Ruby won't have to worry about GLib as they have their own standard libraries which provide similar functionality.
GTK+ and associated libraries implement an object oriented approach through GObject. How this works isn't important just yet, and different programming languages will reveal this to you differently, however, it's important to understand that GTK+ uses object orientation (yes, even in C).
Every piece of a GTK+ GUI is comprised of one or more "widgets" which are objects. All widgets will be derived from a base widget called GtkWidget. For example, an application's window is a widget called GtkWindow. The toolbar within that window is a widget called GtkToolbar. Although a GtkWindow is also a GtkWidget, a GtkWidget is not neccesarily a GtkWindow. Child widgets are derived from their parent objects to extend the functionality of that object. These are standard OOP (object oriented programming) concepts (hint: Google search "object oriented programming" if this is a new concept).
We can look at any widget in the GTK+ reference documentation to see which objects it is derived from. In the case of GtkWindow, it looks something like this:
GObject
+----GInitiallyUnowned
+----GtkObject
+----GtkWidget
+----GtkContainer
+----GtkBin
+----GtkWindow
As you can see, a GtkWindow is derived from GtkBin which is derived from GtkContainer, and so on. For your first application, you don't need to worry about anything above the GtkWidget object. The reason this heirarchy is so important is because when you're looking for functions, properties, and signals for any particular widget, you need to realize that the functions, properties, and signals of it's parent objects apply to it as well. In part 2, this will become even more apparent when writing code for this example application.
We also begin to see a naming convention emerge. This is pretty handy. We can easily tell what library an object or function is from. All objects beginning with Gtk are from GTK+. Later, we'll see things like GladeXML which is part of Libglade or GError which is part of GLib. All objects (and thus Widgets) are in camel case. The functions which manipulate these objects are in lower-case with underscores for spaces. For example, gtk_window_set_title() is a function to set the title property of a GtkWindow object.
All the reference documentation you will need is available online from library.gnome.org/devel/references, however, it is much easier to use Devhelp which is likely available as a package for your distribution. Devhelp allows you to browse and search the API documentation for the libraries you have installed on your system (assuming you install that libraries documentation as well).
More information on GTK+ and Glib:
- Foundations of GTK+ Development (book)
- GTK+ 2.0 Tutorial
- GTK+ - The GIMP Toolkit
- GLib Reference Manual
- GTK+ Reference Manual
- GTK+ 2.0 Tutorial (C Programming)
- PyGTK+ 2.0 Tutorial (Python Programming)
Introduction to Glade3
Glade is a RAD (Rapid Application Development) tool for designing GTK+ applications. Glade is a GTK+ application itself. It is simply a piece of software developers use to simplify the process of laying out an application's interface. Glade creates what will hereforth be refered to a s a "glade file". A glade file is actually an XML file which describes the heirachy of the widgets comprising the interface.
Glade originally generated C code to build the GUI (and you'll still find examples and tutorials doing this). This was later discouraged in favor of using a library, Libglade, to build the interface at run time. And finally, as of Glade3, the old method has become deprecated. That means the ONLY thing glade does is allow you to generate a glade file which describes how the GUI is going to be built. This allows more flexibility with the developer, prevents having to re-compile applications when a minor interface change is needed, and allows more programming languages to be used with Glade.
Glade3 has had significant changes since previous versions such as Glade2. Glade3 has been available for some time and you shouldn't have any problems obtaining it. The package manager for your distribution (yum, aptitude, etc.) should have Glade3 available. You should note however, that the package will have 3 in it. Where 'glade' may be the name for the old package, 'glade-3' or 'glade3' will be the package name for the new version on which this tutorial is based. Glade is also available from source at glade.gnome.org.
Getting Familiar with the Glade Interface
Start up Glade and let's get familiar with it's interface. I will be referring to various aspects of Glade by the names described here. On the left hand side is the "Palette". The Palette is like that of a graphics editing application. It is a palette of GtkWidgets which you can use to design your application. In the middle area (which is empty when you first start Glade) is the "Editor". This is where you see your design in progress. On the right hand side is the "Inspector" on top and the widget "Properties" below that. The Inspector shows your design as a tree allowing you to access and view the heirarchy of the widgets making up your design. We manipulate various properties of widgets in the Properties tabs, including specifying callback functions for signals (explained later).
So, the verfy first thing we're going to do, is create a Toplevel widget and save our file. To do this, Click on the GtkWindow icon
in the Palette under the 'Toplevels' section. You should notice a gray box show up inside the Editor area of Glade. This is the workable area of a GtkWindow. The titlebar, close button, etc. will be added to the widget by the window manager (ie: GNOME) so we don't see it while editing. We will always start with a toplevel widget in Glade, typically a GtkWindow.
Before going further, save the file as "tutorial.glade".
Now the file you just saved, "tutorial.glade", is an XML file. If you were to open it up in a text editor, it would look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.4.0 on Tue Nov 20 14:05:37 2007 -->
<glade-interface>
<widget class="GtkWindow" id="window1">
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<child>
<placeholder/>
</child>
</widget>
</glade-interface>
As you can see, it's just a simple XML file. In part2 we will be using C with Libglade to parse this XML file and generate the UI at run-time. Being XML, this could just as easily be parsed by a Python program or any other language. As we continue to work in Glade, this file will be updated to describe our interface in this XML format any time we save. Exit out of your text editor and return to Glade.
Manipulating Widget Properties
The Editor of Glade now shows an empty GtkWindow widget. We are going to manipulate some of the widget's properties. If you look in the Properties pane of Glade, you will see 4 tabs: 'General', 'Packing', 'Common', and 'Signals'. Let's talk about the first 2 tabs. GtkWidgets typically have various properties which manipulate how they function and/or how they are displayed on the screen.
If you look at the reference documentation for a GtkWidget and scroll down to the "Properties" section, you'll see a list of the properties for a GtkWindow. These are typically the properties which appear in the 'General' tab of the Glade properties pane and will vary from widget to widget. The name property exists for every widget in Glade and is what we will use to reference the widget when it comes time to write code for the application. Change the 'name' property of this GtkWindow from "window1" to "window". Then, add the text "GTK+ Text Editor" to the 'Window Title' property.

We'll discuss the 'Packing' tab in a bit, but first, let's look at the 'Common' tab. This tab also contains properties which belong to our GtkWindow, however, we don't see them in the reference documentation for GtkWindow. This is because this is where we set properties which areinherited from parent objects. Looking at the reference documentation for a GtkWidget in the section called "Object Hierarchy", you'll see the objects from which GtkWindow is derived. Click on the GtkContainer link to jump to the reference documentation for a GtkContainer. You'll notice that GtkContainer has a property called "border-width" and we have a property in Glade for "Border width" at the bottom of the 'Common' tab. We'll learn more about what a container widget is later, however, this demonstrates how important that object heirarchy is. Since many widgets are derived from GtkContainer, Glade puts it's properties into the 'Common' tab.
In the "Object Hierarchy" section of the reference documentation for a GtkContainer you'll see that it is derived from a GtkWidget. Now click the GtkWidget link in to jump to the reference documentation for a GtkWidget. The GtkWidget has a bunch of properties, many of which are also shown in the 'Common' tab of Glade's Properties pane. These are properties which are common to all GTK+ widgets since all GTK+ widgets are derivitives of GtkWidget.

Specifying Callback Functions for Signals
Objects emit a "signal" when something that might be useful to the programmer happens. These are similiar to "events" from Visual Basic. If a user does anything within your GUI, chances are they are emitting signals. As a programmer, you choose which signals you want to capture and perform a task, and connect a callback function to that signal.
The first signal we'll learn, and the one which you'll use in just about every GTK+ application you write, is the "destroy" signal emitted by GtkObject. This signal is emitted whenever a GtkObject is destroyed. This is important, because when the user closes the window through the little 'x' up in the title bar or any other means, the widget is destroyed. We want to capture this signal and exit our application properly. This is better illustrated when we write code for this GUI, however, for now, let's just specify the function we want to call when the "destroy" signal is emitted for our GtkWindow.
Look at the 'Signals' tab in the Glade Properties pane. You see a tree view where GtkWindow and each of the objects from which it is derived are listed. If you expand the GtkObject, you'll see all the signals emitted by GtkObject. These correspond to the reference documentation for a GtkObject in the "Signals" section.
Under the 'Handler' column, click the gray text "<Type here>" to begin editing. Select 'on_window_destroy' from the drop down and then hit ENTER. We can type anything we want here, however, glade provides a drop-down list of some of the mroe common callback function naming conventions. How this value is used depends on how the programmer connects signals in the code, however, for this tutorial, we want the GtkWindow's "destroy" signal to be associated with the handler string "on_window_destroy". We'll look at this closer in Part 2.

At this point, we actually have a working GUI. We could write a few lines of code in C, Python, Ruby, or any number of programming languages which would show our empty window and then properly terminate when we clicked the "x" in the titlebar. For this tutorial however, I will be showing you how to build the entire GUI in Glade3 before writing any code. However, to satisfy any possible curiosity, if you would like to see what a simple program looks like that would implement this Glade interface so far...
In C
/*
First run tutorial.glade through gtk-builder-convert with this command:
gtk-builder-convert tutorial.glade tutorial.xml
Then save this file as main.c and compile it using this command
(those are backticks, not single quotes):
gcc -Wall -g -o tutorial main.c `pkg-config --cflags --libs gtk+-2.0` -export-dynamic
Then execute it using:
./tutorial
*/
#include <gtk/gtk.h>
void
on_window_destroy (GtkObject *object, gpointer user_data)
{
gtk_main_quit ();
}
int
main (int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init (&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "tutorial.xml", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));
gtk_widget_show (window);
gtk_main ();
return 0;
}
In Python (note: you must set the 'visible' property of 'window' to "Yes" in the 'Common' properties tab in Glade)
#!/usr/bin/env python
# First run tutorial.glade through gtk-builder-convert with this command:
# gtk-builder-convert tutorial.glade tutorial.xml
# Then save this file as tutorial.py and make it executable using this command:
# chmod a+x tutorial.py
# And execute it:
# ./tutorial.py
import pygtk
pygtk.require("2.0")
import gtk
class TutorialApp(object):
def __init__(self):
builder = gtk.Builder()
builder.add_from_file("tutorial.xml")
builder.connect_signals({ "on_window_destroy" : gtk.main_quit })
self.window = builder.get_object("window")
self.window.show()
if __name__ == "__main__":
app = TutorialApp()
gtk.main()
Again, I'm not going to go over the details of the code used to implement this GUI in this part of the tutorial, but instead focus on using Glade3. However, you can see that implementing an interface designed in Glade is just a few lines of code in the language of your choosing!
Adding Widgets to the GtkWindow
If you recall from the reference documentation for a GtkWindow, the GtkWindow is a derivative of GtkContainer. Widgets derived from GtkContainer are container widgets, meaning they can contain other widgets. This is another fundamental concept of GTK+ programming. If you come from a Windows programming background, you may be expecting to just drop a bunch of widgets onto the window and drag them around into the position you want them. But this is not how GTK+ works--and for good reason.
GTK+ widgets "packed" into various containers. Containers can be packed into containers into containers and so forth. There are various packing properties which effect how space is allocated for widgets packed into containers. Through these packing properties and nesting containers, we can have complex GUI designs without having to write code to handler the resizing and re-positioning of our widgets.
This is probably one of the more difficult concepts for a new GTK+ developer, so let's just see it in action!
The GtkWindow is a derivative of the container GtkBin which is a container that contains only one child widget. But this text editor is going to have 3 main sections; a menu bar, a text editing area, and a status bar. Therefore, we use a fundamental GTK+ widget, the GtkVBox. The GtkVBox (vertical box) is a container widget which can contain any number of child widgets stacked up vertically like rows (GtkHBox is the horizontal equivelant).
Click on the GtkVBox icon
in the Glade Palette under the 'Containers' section. You'll notice that the 'Select' toolbar button on the top of the Glade window is no longer depressed and your mouse cursor is the GtkVBox icon with a plus (+) sign when hovering over the Glade Editor. This means you are ready to drop the GtkVBox somewhere. Click in gray area of the Editor which is the empty space of the GtkWindow.
A dialog box pops up asking you for the 'Number of items'. In this case we want 3 rows, so leave it as 3 and click 'OK'.

You should see the GtkWindow in the Editor divided into 3 rows now. These are the 3 empty child widgets of the GtkVBox we just added. You should also notice that the 'Select' toolbar at the top of Glade is once again depressed--allowing you to select widgets within the Editor.

Next, click on the GtkMenuBar icon
in the Glade Palette under 'Containers'. Drop this one in the top row of the GtkVBox you just added.

Now click on the GtkScrolledWindow icon
in the Glade Palette under 'Containers'. Drop this one in to the middle row of the GtkVBox. When you do that, it may not seem like anything has happened. However, you should notice that that middle row looks selected. It's not--the GtkScrolledWindow is.The reason you don't see anything, is because a GtkScrolledWindow doesn't have any initial visible components. It's a container which will provide scroll bars when it's child widget gets too large. We'll need this for our text editing widget.

Click the GtkTextView icon
in the Glade Palette under 'Control and Display'. Drop this one right on top of that GtkScrolledWindow (the middle row). We have now just added the GtkTextView to the GtkScrolledWindow which was added to the GtkVBox.

Finally, click on the GtkStatusbar icon
in the Glade Palette under 'Control and Display' and drop it into the bottom row.

And there you have it; the basic layout of our GTK+ text editor. If you look at the Glade Inspector you will see the parent-child relationship of our design.

The Inspector will come in handy. You cannot always click on a widget in the Editor as it might not be visible. For example, you cannot click on the GtkScrolledWindow we added, because you can only see it's child, the GtkTextView. Therefore, if you need to change the properties of "scrolledwindow1", you will have to select it in the Inspector.
I mentioned earlier how "packing" is an often frustrating concept to new GTK+ developers. Therefore, I'm going to show you first hand how various packing effects the layout of your design.
How Packing Effects the Layout
When you look at the interface we've designed so far, you may take for granted how "smart" Glade was. How did it know we didn't want to make the status bar taller? Moreover, if you resize the window, how does it know that the text editing area grows to fill the new vertical space? Well, Glade guessed. It applied default packing options which are often what we want--but not always.
The best way to learn about packing is to play around with packing properties in Glade as you can see the effects in real time. First, a quick description of the applicable properties. Once you get a feel for GTK+, you may want to read more on packing and space allocation.
- homogeneous: A property of the container widget which when set, tells GTK+ to allocate the same amount of space for each child.
- expand: A property of the child being packed specifying if it should recieve extra space when the parent grows.
- fill: A property of the child being packed specifying whether any extra space should be given to the child or used as padding around the child.
Let's look at the default packing for our design. The GtkScrolledWindow has "expand"=TRUE which means it recieves extra space when the parent grows, and it has "fill"=TRUE which means it uses that extra space it recieves. This is how we want it to work.
| Widget | Property | Value |
| GtkVBox "vbox1" | homogeneous | FALSE |
| GtkMenuBar "menubar1" | expand | FALSE |
| fill | TRUE | |
| GtkScrolledWindow "scrolledwindow1" | expand | TRUE |
| fill | TRUE | |
| GtkStatusbar "statusbar1" | expand | FALSE |
| fill | TRUE |
Now, let's see what homogeneous does. Select the GtkVBox in the Glade Inspector and change it's "Homogeneous" property under 'General' properties tab to "Yes". Now the parent, "vbox1", allocates the same amount of space to each of it's children. Since all 3 child widgets have "fill"=TRUE, they fill up this extra space allocated to them.

Set the "Homogeneous" property back to "No".
Click on the GtkScrolledWindow "scrolledwindow1" in the Glade Inspector and set the "Expand" property in the 'Packing' properties tab to "No". Now none of the child widgets will recieve the extra space when the GtkVBox grows. I've highlighted the 3 children in the image below to illustrate this. Each of the child widgets is it's initially requested size. The extra space allocated to the GtkVBox is simply unused since none of it's children want it (but still belongs to the GtkVBox).

Now set the "Expand" property of the GtkScrolledWindow back to "Yes" and change the "Fill" property to "No" instead. This time, the extra space is allocated to the GtkScrolledWindow since "expand"=TRUE, however, the GtkScrolledWindow doesn't use the space it was allocated since "fill"=FALSE.

Set the "Fill" property back to "Yes" to restore our original packing properties.
I know it seems odd at first, but as you continue to work in Glade, you'll start to pick up on how these packing properties work and once you've conquored that part of the learning curve, you'll be amazed at how little work you have to do related to the position and size of your GUI's elements.
Editing the Menu (or Toolbar)
Glade3 comes with a new Menu and Toolbar editor. Although we aren't using a GtkToolbar in this tutorial, the process is very similar to that of the GtkMenubar. We will use the Glade3 Menu Editor to remove many of the items we won't be using and to specify signal handlers for the menu items we will be using.
Although you can manipulate the properties and signals of GtkMenuItems from the standard Glade properties pane and can remove items from the Glade Inspector, the Glade3 menu editor provides a simpler way to edit your application's menu.
Select the GtkMenuBar by clicking it in the Glade Editor or in the Glade Inspector. Then right-click and select 'Edit...' from the popup menu. This will launch the menu editor.

The menu editor contains properties just like Glade's main Properties pane and signals at the bottom just like the 'Signals' tab of Glade's main Properties pane. The main difference in the editor is the tree view on the left. It allows you easily add and remove menu items and drag them around. Go ahead and remove the one labeled "_View". This is the only menu item which Glade generates that we won't be using in this tutorial.
For the remaining menu items we will need to do a few things. We need to rename them so that we have a clean, legible way to reference them in our source code. Then, we'll make some changes as a work around to a bug that might effect some readers using GTK+ 2.12.9 or below (Bug #523932), and finally we'll specify signal handlers. The steps for each of the menu items will be the same, so I'll just walk you through the 'New' menu item. Remember, this is all done in the menu editor but can also be done using the Inspector and Properties pane in glade.
- Click on the new menu item 'gtk-new'
- Change the 'Name' property under 'Menu Item' to "new_menu_item"
- Change the 'Label' property under 'Properties' to "_New" (note the underscore, that's an accellerator)
- Change the 'Stock Item' property under 'Properties' to "None"
- Click on another menu item such as 'gtk-open' and then click back to 'gtk-new' to refresh the properties (Bug #533503)
- Change the 'Stock Image' under 'Internal Image Properties' to the 'New' image
- Specify a handler for the "activate" signal callled "on_new_menu_item_activate" (This is done just like before, where we click the tree view to get a drop down list of possible signal names)

Glade Menu Editor before changes

Glade Menu Editor after changing 'New' menu item
Final Touches to the Main Window
It's never very easy to understand references to things like "textview1", "textview2", etc. when you're coding. So now that you know how to set properties, change the names of the following widgets (remember, the name is in the 'General' tab of the Properties pane):
- Rename "textview1" to "text_view"
- Rename "statusbar1" to "statusbar"
And just to make it look a little nicer when the scroll bars are visible, let's add a a border and shadow to the GtkScrolledWindow
- Change the "Shadow Type" to "Etched In' in the 'General' properties tab for "scrolledwindow1"
- Change the "Border Width" to 1 in the 'Common' properties tab for "scrolledwindow1"
- Change the "Left Margin" to 2 in the 'General' properties for "text_view"
- Change the "Right Margin" to 2 in the 'General' properties for "text_view"
The finished glade file can be downloaded here: tutorial.glade
Getting Additional Help Using Glade
If you have additional questions about using Glade, you can always ask on the glade-users mailing list or post your question in the GTK+ Forums.
What Next?
In GTK+ and Glade3 GUI Programming Tutorial - Part 2 I will talk a little bit about choosing a programming language to implement the GUI we just created.
If you don't want to read along and would rather just see the final implementations:
- Glade XML file describing GTK+ text editor GUI (tutorial.glade from part 1)
- GtkBuilder XML file descirbing GTK+ text editor GUI (tutorial.xml from part 2)
- GTK+ text editor implemented using C (main.c from part 4)
- GTK+ text editor implemented using Python (tutorial.py from part 4)
90 Comments about "GTK+ and Glade3 GUI Programming Tutorial - Part 1"
RSS Feed
all i need is i want a window with two buttons one for client side and other for server side so that if i click on respective buttons it has to link to the terminal so that i can run the respective programm.
please hep me.
thanks in advance,
Gagan
Thanks for this good tutorial, actually I have followed the same procedure using Glade-3 , i am getting the warnings at run time like "Could not find signal handler 'on_window_destroy'", eventhough the function is defined .
Can any one please let me know the reason.
Thanks
Jagadish
Do you have a tutorial that covers radio buttons and groups?
I am going nuts trying to figure out how to make them work.
Thanks,
Mike P.
gtk_builder_add_from_file (builder, "tutorial.xml", NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
// it does not match with the name
// in xml , it's "windows1" !
Would you mind to allow that I copy and translate these articles(GTK+ and Glade3 ...tutorial Part 1, 2, and 3) into korean in my blog?
This error hangs up a bunch of tutorial users. The easiest quick fix:
1. In Glade, choose the menu item Edit->Preferences
2. On the first line, "Project File Format", choose "GtkBuilder" instead of "Libglade"
3. Save and run.
This works for at least the first sample of the tutorial.
Thanks!
Thanks!
I keep getting this error.
editor.window.show()
AttributeError: 'NoneType' object has no attribute 'show'
I solved it by inserting: True into xml file.
But still am I the only one?
Anyway, as said cool stuff, thanks
I do have a question. When setting up the handlers, I have a callback function, but I am not sure what all is going to be passed to the handler. Currently I am learning python. Other examples show mycallback(self, widget, data=None) But is that the same for all callbacks?
Thanks again for a very good tutorial.
My name is Simon.
I have a lot of problems with gtk+/c and glade.I spent a lot of time to do my application in glade which can load my png picture and then my program have to rotate this image.I would want to rotate this image by different angle.I want to choose angle by myself in GUI.Could you please help me?I found some of your tutorial about glade and gtk+ but I need some help.Do you have any similar application already made.Thank you a lot.
Have a nice time
Simon
I already got the solution.
thanks
here is the error:
(Part1:9711): Gtk-CRITICAL **: gtk_widget_show: assertion `GTK_IS_WIDGET (widget)' failed.
I'm using Glade 3.4.5. I modify the original code from tutorial.xml to tutorial.glade and I also did copy paste the tutorial.xml from this page to my sample project and I still have the same result.
When I was in ubuntu 9.04 and tried this tutorial, its worked. But when I downgraded to ubuntu 8.10 I have that problem.
Here is the summary of my environment : Ubuntu 8.10, Codeblocks 8.02 and Glade 3.4.5
I hope you guys can help me. Its very frustrating trying to work on this for almost a day :(
I have a question thought.
I have a memory leak that I can solve yet so im going back to the basics.
In your code you destroy the builder object with: g_object_unref (G_OBJECT (builder));
But GTK manual say: "Never call g_object_unref() unless you have previously called g_object_ref(), even if you created the GtkObject."
So my question is, why are you using g_object_unref and not gtk_object_destroy()?
From the manual:
Pass the flag `-export-dynamic' to the ELF linker, on targets that
support it. This instructs the linker to add all symbols, not only
used ones, to the dynamic symbol table.
Me, too. Well, the Windows-version has a Built-Button. I was very surpirsed I couldn't find on Galde for Ubuntu/Linux.
I did the same. I saved with Filtetyp Libglade, which you can set on saving progress. Well I got problems with the next commandoline. Creating the tutorial.xml File works fine.
gcc -Wall -g -o tutorial main.c `pkg-config --cflags --libs gtk+-2.0` -expo
gcc: main.c: No such file or directory
gcc. ....blabla
Someone here who can tell me how to create the c-files anyway?
PS: This is such a good detailed Tutorial I would like to go throug.
I have Glade 3.6.2 installed on my system and I would like to know if the part 4 of the tutorial will be based on 3.6 or 3.4?
I would like to know if I should start working with 3.6 or 3.4, because I cannot find anything about gtkbuilder or glade 3.6.Is it too soon to start with 3.6?
Thank you
I wonder why the glade can't save to XML directly? That save extra step to convert.
Thanks for your post and unfortunately I have to say your sample program in the tutorial doesn't compile, which is very disappointing to beginners. Last time I tried once with glade3.4.5 and find a problem (last Nov.) and this time I find another problem. Being a little more diligent this time, I find out there is a little bug -- the gtk-builder-convert and the glade file mismatch with each other.
Below is the error on my platform :
Platform :ubuntu 9.04 32bit on amd64, python 2.6.2, glade 3.6.3, gtk-builder-convert version unknown.
Traceback (most recent call last):
File "/usr/bin/gtk-builder-convert", line 756, in
sys.exit(main(sys.argv))
File "/usr/bin/gtk-builder-convert", line 744, in main
conv.parse_file(input_filename)
File "/usr/bin/gtk-builder-convert", line 161, in parse_file
self._parse()
File "/usr/bin/gtk-builder-convert", line 233, in _parse
assert glade_iface, ("Badly formed XML, there is "
AssertionError: Badly formed XML, there is no tag.
Looking at the /usr/bin/gtk-builder-convert and I find out that the gtk-builder-convert expects to find 'glade-interface' and try to convert it to 'interface' while the .glade file has only 'interface' instead of 'glade-interface'. The problem may be the gtk-builder-convert is out-of-sync or out-of-date.
The Fix: just comment out the lines from 232 to 243 using """ will skip the conversion.
My suggestion to you Micah is that please re-test the sample program on each release of glade and gtk-builder-convert. If you are too busy, ask for help. Glade3 is a very good tool for RAD and you don't want to discourage your new users.
Thanks very much.
I'm trying to get a little fancier with my gui specification, changing the size and color of elements and getting slightly frustrated at the lack of documentation on glade3. Someone should start a documentation wiki.
Now, I apologize for being nitpicky, and I only mention it assuming you would like the correction: You have a tendency to misuse the word effect. As a general rule effect is the noun, and affect is the verb. There is an exception: you can effect a change for example, which can be parsed as 'putting a change into effect'. So you could say: "lsd affects my perception", or "lsd effects a shift in my perception", but not "lsd effects my perception".
But i have one question : i noticed that the C language version (main.c) seems to do a kind of binding to the tutorial.xml just created with GtkBuilder . And effectively, i then noticed that without the tutorial.xml in the folder, the application doesn't even launch .
is there a nice and easy way to get the application launching without the need of the tutorial.xml ? (Since i'm not very used to C programming, but instead Java, i don't want to get practise of too sophisticated programming ... and i'm not sure that programming in Java for GTK+ give the better performances) .
Thank you very much
i have noticed something though, when i try to compile glade files into xml (using gtk-builder-convert) i always get an error complaining something about no interface tage:
AssertionError: Badly formed XML, there is no tag.
when i compared your glade file to mine, i noticed that my glade file has "" where yours had "". of course changing it to fixed the problem.
just though i'd share this with you people.
PS: i'm using 3.5.4 & thanks again for the tutorial :)
iam myself having trouble to assign a command to 'checkbutton' in glade.
could you give a simple tutorial about how to assign a command to 'checkbutton' ?
for example if user hit ok button it will check if the checkbutton is checked or not,
if checked then the checkbutton command will be execute.
I got the
AttributeError: 'NoneType' object has no attribute 'show'
when I called get_object("window"), because Glade actually created my window with an ID of "window1" instead of "window". Try matching the ID in your xml file.
I have the same issue as benthurston. Running ubuntu hardy on amd64. I am using glade 3 with gtk2.12. it reported fixed but still there.
frank,
Thanks for the tutorial. I had a minor problem but figured it out after a few minutes.
When changing the properties of the menu items "label" is grey and un-accessible. I figured out that to be able to change the label I had to set stock item to none first. So you might want to switch steps 3 and 4. That was confusing to a newb and I thought something was wrong with my installation or I was missing a library or something since that is so often the case in Linux. It's a minor thing so I'm not complaining. I'm just glad to have a starting point to learn how to use glade. I haven't found much in the way of absolute beginner turoials.
Thanks
What to do ?
Thanks for this tutorial.
I tried the example you gave but I had these warning
(Glade_tutorial:1118): Gtk-WARNING **: Could not find signal handler 'on_window1_destroy'
(Glade_tutorial:1118): Gtk-WARNING **: Could not find signal handler 'on_New_menu_item_activate'
(Glade_tutorial:1118): Gtk-CRITICAL **: gtk_widget_show: assertion `GTK_IS_WIDGET (widget)' failed
and I can view the small window that I created throw this tutorial.
What to do ?
Thanks for such a well written tutorial.
Had a couple of questions:
1. How do I create the "minimize", "maximize" and "close" buttons?
2. If I have a text box, how do I make the background fill with default color, instead of it being white background?
Look forward to part 4 of the tutorial.
but i have, one and simple question(not for me!)
how can i add two "entry" with numbers and put the result in a third "entry"
entry3=entry1+entry2...??
please, i hope you can help me
thanks a lot!
I've found plenty of gtkBuilder discussion via Google (and your name has popped up many times Micah), but I can't see any rationale for developing a new glade/xml parser?
Libglade has been used to parse .glade files historically, however, GTK+ recently included GtkBuilder (as of GTK+ 2.12) and that is intended to replace libglade. Both the GTK+ developers and the glade developers are working hard on this transition, however, until Glade 3 can properly save in GtkBuilder format (The developers told me their hoping to have this in the 3.6 series), and until GtkBuilder is fully matured, we have to use the gtk-builder-convert script.
I believe it's better to go through the extra step (running gtk-builder-convert) and use GtkBuilder than to learn something that will be deprecated in the very near future.
I wonder why this command is required and why doesn't/can't glade write the xml file directly? Micah, why don't you just read the glade file using gtk.glade.XML()?
pc:~/pygui myglade.py
Traceback (most recent call last):
File "./myglade.py", line 20, in
mine()
File "./myglade.py", line 14, in __init__
builder.add_from_file('myglade.glade')
gobject.GError: Invalid root element: 'glade-interface'
I'm on kubuntu 8.04 (could be a packaging dependency error?). Any ideas?
I'd tried e-mailing you this, but the contact information at the bottom bounced. In any event:
1st thanks for the tutorial. I've been able to get past the
gtk-builder-convert step, and compiled your sample main.c (just after
creating the main window, the menubar, and status bar, and text widget
at the beginning of the tutorial). No errors were reported in any of
these steps (at least not after patching gtk+ to fix the child image
issue).
However, running the tutorial leads to it hanging. The debugger shows
the problem is that
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
returns null.
Any suggestions? I'm running garnome's svn trunk gnome which means gtk
+-2.13.0 and glade-3.5.2. The glade file is attached.
Thanks in advance
Here's what's in the attachement:
GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_STRUCTURE_MASK
GTK_WINDOW_POPUP
Text Editor
True
True
True
_File
True
True
True
gtk-new
True
True
True
gtk-open
True
True
True
gtk-save
True
True
True
gtk-save-as
True
True
True
True
gtk-quit
True
True
True
_Edit
True
True
True
gtk-cut
True
True
True
gtk-copy
True
True
True
gtk-paste
True
True
True
gtk-delete
True
True
True
_View
True
True
_Help
True
True
True
gtk-about
True
True
False
True
True
1
True
2
False
2
I have also updated the section of this tutorial in which the Menu is created to include a work-around for this bug.
I had the same problem on Ubuntu Hardy. I'm a complete newb to Glade but have some experience that helped me fix this bug. It's a breeze. Check out the comment I added to the bug report: https://bugs.launchpad.net/ubuntu/+source/gtk+2.0/+bug/214267/comments/3
Basically, it appears (to a newb like me) that Glade is adding an extra property that is messing us up. Comment out the property and everything compiles nicely.
As I say, I'm a complete newb so this may only be the BEGINNING of the problem, but at least we can compile the tutorial up to this point. Hopefully Micah will have the next part done soon so that we can make our menus DO SOMETHING.
Great tutorial - I'm up and getting it in under a half hour. And I've never coded for Linux before. Very well done.
Could you post this question at gtkforums.com and include your platform/distribution, verison of GTK, and version of Python.
This is relatively new to me. Looks similar to this bug: https://bugs.launchpad.net/ubuntu/+source/gtk+2.0/+bug/214267
As much information as possible would help.
gives me the error message:
UnboundLocalError: local variable 'child' referenced before assignment
even when i use your included glade file.
is there a list of all libglade functions available on any website......i mean for eg. all functions required to read and write to a textbox, etc.
but there is a little fault with the C code....not
<code>
GtkWidget *window;
</code>
but
<code>
GtkWidget *window1;
</code>
There is so few tutorial about gtk/glade3. You done it!
Is it possible to translate your tutorials in french. If yes, I can do it!
So what is the easiest way? Have you got the same versions in xml for example?
Seen you soon...
However, when I tried to run the c version of your program I got the error "Invalid root element: 'glade-interface'", I tried with both the xml file I generated, and the one you provide for download, and cannot find a similar error on any forums. Any ideas as to what the problem is?
Thanks!
Glades menu editor is similar to the signal tab of the properties pane in glade. The signals belong to the widget at the top of the tree view such as GtkMenuItem.
I'm start to learn something about GTK+ and Glade3 Design.
but so little articles on this top in my native language,chinese.
your articles makes me clear about some concepts ,and very useful for me.
thank U again!
Can you tell me what type of widget is the one that contains signals and handlers in glade's menu editor?
Thank you in advance
Sorry..... <^_^>
Traceback (most recent call last):
File "tmp.py", line 18, in ?
app = TutorialApp()
File "tmp.py", line 15, in __init__
self.wTree.get_widget("window").show()
AttributeError: 'NoneType' object has no attribute 'show'
Is there a library or something I'm missing?
Checking the gtk.AboutDialog gifs no explanation.
Select the GtkMenuBar by clicking it in the Glade Editor or in the Glade Inspector. Then right-click and select 'Edit...' from the popup menu. This will launch the menu editor.
=======================================
but when I right click in Glade Inspector or Glade Editor I have no Edit option.I can see only select,cut,copy,paste,delete.There is no edit.So i did those changing in properties.I use glade-3 on Debian testing platform.Anyway thanks for tutorial it has helped me to begin to understanding those GUI programing.
I'm sorry. That little example was sort of half-way what the next sections will cover. GtkBuilder is what I'll be using in subsequent parts of this tutorial as it's a newer way of implementing glade interfaces. In this case, I used LibGlade as it's more common still. I have fixed and tested the example in this section.
I was trying to compile the code you gave in C and the compiler fails with the error "'gxml' undeclared".
I don't know this stuff but tried declaring it in main as "GladeXML *gxml" and it compiles but then when I run the program it segfaults. Am I missing something?
Just wanted to add my thanks, too. My son has helped inspire me to begin programming again. I used to create programs non-professionally using C and Delphi in Windows, but that was a while ago. It got too expensive trying to keep upgrading. Open source is a great and inexpensive way to develop and build programming skills. And your tutorial is an excellent introduction to Glade3. My son is getting started in programming. I'm going to point him to this tutorial as well. After all, what good is a program if it doesn't have a well designed GUI. Glade makes that part much easier. Many thanks to you for the tutorial(s) and to all of the developers who have made all of this possible.
"how can we change the GtkTextView to a GtkSourceView 2.0 widget in glade3 ? I did not find any button for it. Maybe I shall edit the glade file manually ?"
Rather than adding custom widgets to glade, what I typically do, is leave that blank. That is, I'll leave the scrolled window there but not put anything into it. Then, I add the GtkSourceView to the scrolled window programatically.
First thanks for those nice tutorials, they will be very helpfull for me as I'm just starting working with the Gtk toolkit.
One question : how can we change the GtkTextView to a GtkSourceView 2.0 widget in glade3 ? I did not find any button for it. Maybe I shall edit the glade file manually ?
Thanks again.
To me, this is a very good start to make people interested in this such kind of programming/GUI building, which hasn't been given the deserved attention until now.
Leave a Comment about "GTK+ and Glade3 GUI Programming Tutorial - Part 1"