Tutorial: Building GUIs for Ruby programs with Glade
March 2nd, 2007
Glade has become a relatively popular tool for developing user interfaces for GTK+. The cool thing about Glade is the fact that it generates the interface in an XML dialect that can be imported by several programming languages, including C/C++, Python, Mono and Ruby among others.
A programmer can design an interface in Glade, and specify signals (events) for each of the components in the graphical user interface. Using tools like ‘ruby-glade-create-template’, one can have the method signatures generated automatically, then just fill in the blanks and end up with pretty nifty applications. Pretty neat, huh?
The problem? Finding the necessary information to get started.
In this tutorial we will use Ruby and Glade in order to generate a simple, but graphical, ‘Hello World!’ program. I’m assuming you have Glade 3 installed on your system and all the necessary libraries (libglade, et al).
When you fire up Glade, you’ll see three windows. The Main Window, the Palette, and the Properties Window.
I suggest clicking on View -> Palette Appearance -> Text Beside Icons in order to make it easier to identify components while you get used to the icons.
The first thing we want to do is add a window component to our project and modify its properties to suit our needs. This window object will be a container of our other elements.
In the
Palette, click onWindow. A newWindowobject will appear on screen.Now, we will change some properties.
- In the
Generaltab of thePropertieswindow, click on theNamefield. Change it tomyWindow. (It is important to use names we can remember, as we will need to use them in the code.)- We will leave the
Window Titleproperty blank, as this is what we will be changing toHello Worldin the code.- Set the
Window Positionproperty toCenter.- Set the
Default Widthproperty to400and theDefault Heightproperty to50.
Now, we need to add a signal (event) for quiting our program.
- Go to the
Signalstab in thePropertieswindow.- Expand the
GtkWidgetnode.- Select
delete-eventand typeon_quitin theHandlercolumn.
With this, we have made sure the event signature will be created for us and all we will need to do is fill-in our code.We are now done modifying our Window object. Our next step is adding a Button.
- Go to the
Paletteand add aButtonobject tomyWindow.
By default, the button will take all the window space. (Dont worry about that for now. It can be changed by adding a
Fixedobject to the window.)
- Change the button’s name to
myButtonand change the label toSay Hello!
- Now move into the Signals tab, click on the clicked event, select in
on_myButton_clickedand pressEnter.
We’ve completed the design of the GUI. Save the project.
2. Generate the Ruby template
ruby-glade-create-template helloworld.glade > helloworld.rbNow, if you open helloworld.rb, you should see something similar to this…
With a working template, we are now left with only one task: filling in the necessary code to make our program work.
3. Writing our code
Open up
helloworld.rbwith your favorite text editor.Change the
initializedefinition to look like this:
def initialize(path_or_data, root = nil, domain = nil, localedir = nil, flag = GladeXML::FILE)
bindtextdomain(domain, localedir, nil, "UTF-8")
@glade = GladeXML.new(path_or_data, root, domain, localedir, flag) {|handler| method(handler)}
@mywindow = @glade.get_widget("myWindow")
@mywindow.show
endBasically, we are setting an instance variable
@mywindowwith themyWindowobject returned by Glade. Then, we tell that object to show himself.Now, change the
on_quitmethod definition:
def on_quit(widget, arg0)
puts "This is me leaving."
Gtk.main_quit
endWe are outputting a message before leaving and then asking GTK to shut down this bastard.
Last, define the
on_myButton_clickedhandler:
def on_myButton_clicked(widget)
@mywindow.title = 'Hello World!'
end
Save your file and exit!
4. Execute and Enjoy!
ruby helloworld.rb
Click the button and you should see the
Window Titlechange toHello World!
Who would have thought of that?



















March 24th, 2007 at 2:46 am
Thanks guy, I will have to start using glade more often.
May 3rd, 2007 at 11:22 pm
Appreciate the tutorial, was just what I needed.
August 30th, 2007 at 6:48 pm
I think I\’ve got everything installed on my system: I can run glade and Ruby, I can create the XML file… But where is ruby-glade-create-template located? I can\’t find that script on my machine anywhere.
thanks!
September 26th, 2007 at 11:44 am
is there ruby-glade-create-template for windows systems? [i know its a crappy OS…i’m at work
] google didn’t turn anything up!
January 6th, 2008 at 12:51 pm
Amazing man, thanks!
January 13th, 2008 at 7:23 pm
Very helpful tutorial! Thanks a lot!
January 21st, 2008 at 2:19 am
For those looking for the ruby-glade-create-template command, you need to install ruby-libglade. This is not a ruby gem. On Gentoo, it can be installed via \’emerge dev-ruby/ruby-libglade2\’.
You will also need the ruby gem gettext. This can be installed via \’gem install gettext\’.
January 29th, 2008 at 7:00 pm
Thanks for this great tutorial. It was a perfect start for using GTK+ with Ruby. Well done and thanks a lot!
March 5th, 2008 at 7:23 am
That’s what i was looking for, thanks!
But i’ll suggest extending the generated method, so you can add widgets via glade, regenerate and implement the new methods, whereas the old stuff doesn’t change.