Chapter 8. The Button Widget

Table of Contents

1. Normal Buttons
2. Toggle Buttons
3. Check Buttons
4. Radio Buttons

1. Normal Buttons

We've almost seen all there is to see of the button widget. It's pretty simple. There is however more than one way to create a button. You can use Gtk2::Button::new_with_label() or Gtk2::Button::new_with_mnemonic() (which can be shortened to just Gtk2::Button::new() ) to create a button with a label, use Gtk2::Button::new_from_stock() to create a button containing the image and text from a stock item or use Gtk2::Button::new() with no arguments to create a blank button. It's then up to you to pack a label or pixmap into this new button. To do this, create a new box, and then pack your objects into this box using the usual Gtk2::Box::pack_start() function, and then use the Gtk2::Box::add() method call to pack the box into the button.

Here's an example of using Gtk2::Button::new() to create a button with a image and a label in it. I've broken up the code to create a box from the rest so you can use it in your programs. There are further examples of using images later in the tutorial.

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

# Create a new hbox with an image and a label packed into it and return
# the box.
sub xpm_label_box {
	my ($xpm_filename, $label_text) = @_;

	# create box for image and label 
	my $box = Gtk2::HBox->new(FALSE, 0);

	# now on to the image stuff
	my $image = Gtk2::Image->new_from_file($xpm_filename);
	# Create a label for the button
	my $label = Gtk2::Label->new($label_text);
	# pack the image and label into the box
	$box->pack_start($image, FALSE, FALSE, 3);
	$box->pack_start($label, FALSE, FALSE, 3);


	return $box

# our usual callback function
sub callback {
	my $widget = shift;
	my $data   = shift;
	printf "Hello again - %s was pressed\n", $data;

# main starts here

# create a new window
my $window = Gtk2::Window->new('toplevel');

$window->set_title("Pixmap'd Buttons!");

# it's a good idea to do this for all windows
$window->signal_connect("destroy" => sub { Gtk2->main_quit; });
$window->signal_connect("delete-event" => sub { Gtk2->main_quit; });

# sets the border width of the window

# create a new button
my $button = Gtk2::Button->new();

# connect the 'clicked' signal of the button to our callback
$button->signal_connect("clicked" => \&callback, "cool button");

# this calls our box creating function
my $box = xpm_label_box("info.xpm", "cool button");

# pack and show all our widgets





# rest in the GTK main loop and wait for the fun to begin!


The xpm_label_box() function could be used to pack images and labels into any widget that can be a container.

The Gtk2::Button widget has the following signals:

  • pressed - emitted when pointer button is pressed within Gtk2::Button widget
  • released - emitted when pointer button is released within Gtk2::Button widget
  • clicked - emitted when pointer button is pressed and then released within Gtk2::Button widget
  • enter - emitted when pointer enters Gtk2::Button widget
  • leave - emitted when pointer leaves Gtk2::Button widget