# Example of ruby using gtkbuikder (Gtk::Builder)

**URL:** <https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573>\
**Category:** Ruby-Gnome 2\
**Created:** [May 14, 2010, 2:36am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573 "2010-05-14T02:36:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [May 14, 2010, 2:36am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573/1 "2010-05-14T02:36:45Z")

</div>

Hi, well, now i’m on ubuntu 10,04 LTS, i’m trying to make a hello world  
app, i can do it with pure code but i don’t know how to do it using  
glade 3.x and using Gtk::Builder.

I’ve noticed some examples calling the XML-GLADE file as “ui” extension,  
for me it’s a little strange because glade saves files as “glade”  
extension, should i have to convert “glade” to “ui” extension? how can i  
do that, or the Gtk::Builder.add\_from\_file can call “glade” files too.

What Gtk+'s version is working with ruby? i’ve read is working with  
2.12, and partially 2.14, that’s from de glade documentation, is that  
true or can work with 2.16,

And please, i need an example, of a glade-xml file and it’s ruby code  
calling the user interface.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [May 14, 2010, 9:49am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573/2 "2010-05-14T09:49:08Z")

</div>

Hei Rodrigo,

when you save a file with glade3 you will see, in the save dialog, two  
options in the lower left corner. If you want to load your interface  
definition file with Gtk::Builder you should choose the “GtkBuilder” one  
before saving. The extension doesn’t matter.

As for ruby, if you’re on ubuntu and install the latest ruby/gnome2  
(0.19.3) you should be fine.

A sample GtkBuilder definition file, with a window containing just a  
button, follows:

\<?xml version="1.0"?\> True button True True True 

To load this interface in you ruby program:

builder = Gtk::Builder.new  
builder.add\_from\_file(your\_filepath)

then, if you call Gtk.main you should see your window. Notice that glade  
sets the visible property of windows as FALSE by default so, if you  
don’t change it to true, you won’t see your widget after the Gtk.main  
call.

Anyway: if you plan to use gtk + ruby on ubuntu I can only suggest you  
to use freightrain ([GitHub - bolthar/freightrain: Ruby desktop development made easy · GitHub](http://github.com/bolthar/freightrain) ), both  
because I know the pain of building everything by hand and because i  
wrote it 🙂

Bye!

–  
Andrea D.

> **[GitHub - bolthar/freightrain: Ruby desktop development made easy](https://github.com/bolthar/freightrain)**
>
> Ruby desktop development made easy

> **[using (IMHO)](https://usingimho.wordpress.com)**
>
> Andrea Dallera 's blog

On Fri, 2010-05-14 at 02:36 +0200, Rodrigo DurÃ¡ wrote:

> Hi, well, now i’m on ubuntu 10,04 LTS, i’m trying to make a hello world  
> app, i can do it with pure code but i don’t know how to do it using  
> glade 3.x and using Gtk::Builder.

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [May 15, 2010, 3:02am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573/3 "2010-05-15T03:02:46Z")

</div>

Hi, thank you, it works for me :D, i’m gonna follow your advices XD.

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [October 13, 2010, 3:32am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573/4 "2010-10-13T03:32:29Z")

</div>

Thank you Andrea for that very helpful tip. I have a followup question:

How do I attach events in my ruby code to access the events I create in  
Glade?

Here’s my question exactly:

Using glade, I created a file called “button.glade”. It is just a basic  
window with a button in the middle of it. I set visible to true so it  
would be visible. Then I selected the button object, clicked on the  
“Signals” tab, and set the "clicked event to:

on\_button1\_clicked

In a similar fashion I set the on\_window\_destroy event to  
on\_window1\_destroy.

I saved the file as button.glade with the property set to use libglade.

Then I ran this command:

ruby-glade-create-interface button.glade \> button.rb

It created this skeleton program:

#!/usr/bin/env ruby

# 

# This file is gererated by ruby-glade-create-template 1.1.4.

# 

require ‘libglade2’

class ButtonGlade  
include GetText

attr :glade

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

end

def on\_button1\_clicked(widget)  
puts “on\_button1\_clicked() is not implemented yet.”  
end  
def on\_window1\_destroy(widget)  
puts “on\_window1\_destroy() is not implemented yet.”  
end  
end

# Main program

if **FILE** == $0

# Set values as your own application.

PROG\_PATH = “button.glade”  
PROG\_NAME = “YOUR\_APPLICATION\_NAME”  
ButtonGlade.new(PROG\_PATH, nil, PROG\_NAME)  
Gtk.main  
end

As you can see the methods “on\_button1\_clicked” and “on\_window\_destroy”  
are easy to understand.

How do I make this exact same skeliton program for a GTKBuilder/ruby  
program? I can get the window to pop-up using your advise from your  
last post, but I can’t get the window to call Gtk.main\_quit().

I’m attaching the file button.glade saved in gtkbuilder format. Can you  
make a working file button.rb that has methods to destroy properly, and  
spit out a message when you click the button?

Thanks in advance,  
Eric

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [October 14, 2010, 9:51am UTC](https://www.ruby-forum.com/t/example-of-ruby-using-gtkbuikder-gtk-builder/188573/5 "2010-10-14T09:51:24Z")

</div>

I figured it out from this post:

[http://www.ruby-forum.com/topic/173746](http://www.ruby-forum.com/topic/173746)

My final version is:

#######################  
require ‘rubygems’  
require ‘gtk2’

class VR\_gui  
def initialize  
if **FILE** == $0  
@builder = Gtk::Builder.new  
@builder.add\_from\_file(“button.glade”)  
@builder.connect\_signals{ | handler | method(handler) }  
Gtk.main()  
end  
end

def on\_button1\_clicked  
puts “Button Clicked”  
end

def gtk\_main\_quit  
puts “Gtk.main\_quit”  
Gtk.main\_quit()  
end

end

x = VR\_gui.new

#########################

I never got the on\_window1\_destroy method to work. For some reason when  
I closed the window, it didn’t get called. I had to use gtk\_main\_quit  
instead.
