Example of ruby using gtkbuikder (Gtk::Builder)

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.

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 (http://github.com/bolthar/freightrain ), both
because I know the pain of building everything by hand and because i
wrote it :slight_smile:

Bye!


Andrea D.
http://github.com/bolthar/freightrain
http://usingimho.wordpress.com

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.

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

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

I figured it out from this post:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs