The Button Widget

Normal Buttons

We've almost seen all there is to see of the button widget. It's pretty simple. There is however a couple of ways to create a button.

  1. You can use b = Gtk::Button.new "Some text" to create a button with a label.

  2. You can create a more interesting button if you use b = Gtk::Button.new and then b.add a box. The box might contain a pixmap image and a label, for instance.

Here is an example of the alter option.

Figure 1. Button with an image

require 'gtk'
require 'gdk_pixbuf'

def load_image(file)
    pixbuf = Gdk::Pixbuf.new file
    pixmap, mask = pixbuf.render_pixmap_and_mask
    image  = Gtk::Pixmap.new(pixmap, mask)
end

window = Gtk::Window.new Gtk::WINDOW_TOPLEVEL
window.signal_connect('delete_event') { Gtk.main_quit }
window.border_width 20
window.set_title "Button"

#
# Create a button with an image and a label.
#
button = Gtk::Button.new

hbox = Gtk::HBox.new
button.add hbox

image = load_image "gnu.png"
hbox.pack_start image

label = Gtk::Label.new "Cool!"
hbox.pack_start label

#
# Done
#
window.add button
window.show_all
Gtk.main