Upgraded Hello

Now we take a look at a more interesting helloworld.rb, with a better example of callbacks. This will also introduce us to the next topic, packing widgets.


Notice that we have two buttons connected to the same callback with different arguments.

Figure 2. Hello World 2

require 'gtk'

def greeting(num)
    puts "This is button #{num}"

# Build the GUI

window = Gtk::Window.new(Gtk::WINDOW_TOPLEVEL)
hbox   = Gtk::HBox.new(false,0)

button1 = Gtk::Button.new("button 1")
button2 = Gtk::Button.new("button 2")

hbox.pack_start button1, true, true, 0
hbox.pack_start button2, true, true, 0

window.add hbox

# Callbacks and configuration.

window.set_title    "Two buttons"
window.border_width 10
window.signal_connect('delete_event') { false }
window.signal_connect('destroy') { Gtk.main_quit }

button1.signal_connect('clicked') { greeting(1) }
button2.signal_connect('clicked') { greeting(2) }

# All done.