Getting Started

If you don't have GTK installed already, you must go to http://www.gtk.org and get it. You can obtain the Gtk bindings for Ruby from the Ruby/Gtk website.

Hello World

To begin our introduction to GTK, we'll start with a simple program. This program will create a window with a button.

Figure 1. Hello World

require 'gtk'

window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
button = Gtk::Button.new("Hello World")

window.set_title("Hello Ruby")
window.border_width(10)

# Connect the button to a callback.
button.signal_connect('clicked') { puts "Hello Ruby" }

# Connect the signals 'delete_event' and 'destroy'
window.signal_connect('delete_event') {
	puts "delete_event received"
	false
}
window.signal_connect('destroy') {
	puts "destroy event received"
	Gtk.main_quit
}

window.add button
window.show_all
Gtk.main