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 = "Some text" to create a button with a label.

  2. You can create a more interesting button if you use b = 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 = file
    pixmap, mask = pixbuf.render_pixmap_and_mask
    image  =, mask)

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 =

hbox =
button.add hbox

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

label = "Cool!"
hbox.pack_start label

# Done
window.add button