4. Progress Bars

Progress bars are used to show the status of an operation. They are pretty easy to use, as you will see with the code below. But first lets start out with the calls to create a new progress bar.

$progressbar = Gtk2::ProgressBar->new;

Now that the progress bar has been created we can use it.

Gtk2::ProgressBar->set_fraction($progressbar, $fraction);

The first argument is the progress bar you wish to operate on, and the second argument is the amount "completed", meaning the amount the progress bar has been filled from 0-100%. This is passed to the function as a real number ranging from 0 to 1.

GTK v1.2 has added new functionality to the progress bar that enables it to display its value in different ways, and to inform the user of its current value and its range.

A progress bar may be set to one of a number of orientations using the function

Gtk2::ProgressBar->set_orientation($progressbar, $orientation);

The orientation argument may take one of the following values to indicate the direction in which the progress bar moves:


As well as indicating the amount of progress that has occured, the progress bar may be set to just indicate that there is some activity. This can be useful in situations where progress cannot be measured against a value range. The following function indicates that some progress has been made.


The step size of the activity indicator is set using the following function.

Gtk2::ProgressBar->set_pulse_step($progressbar, $fraction);

When not in activity mode, the progress bar can also display a configurable text string within its trough, using the following function.

Gtk2::ProgressBar->set_text($progressbar, $text);


Note that gtk_progress_set_text() doesn't support the printf()-like formatting of the GTK+ 1.2 Progressbar.

You can turn off the display of the string by calling Gtk2::ProgressBar::set_text() again with undef as second argument.

The current text setting of a progressbar can be retrieved with the following function.


Progress Bars are usually used with timeouts or other such functions (see section on Timeouts, I/O and Idle Functions) to give the illusion of multitasking. All will employ the Gtk2::ProgressBar::set_fraction() or Gtk2::ProgressBar::pulse() functions in the same manner.

Here is an example of the progress bar, updated using timeouts. This code also shows you how to reset the Progress Bar.

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

sub progress_timeout
	my $pbar = shift;
	if ($pbar->{activity_mode})
		# Calculate the value of the progress bar using the
		# value range set in the adjustment object
		$new_val = $pbar->get_fraction() + 0.01;
		$new_val = 0.0 if $new_val > 1.0;
		# Set the new value

	# As this is a timeout function, return TRUE so that it
	# continues to get called
	return TRUE;

# Remove the timer
sub destroy_progress
	my $window = shift;


$window = Gtk2::Window->new('toplevel');
$window->signal_connect(destroy => \&destroy_progress);

$vbox = Gtk2::VBox->new(FALSE, 5);

# Create a centering alignment object;
$align = Gtk2::Alignment->new(0.5, 0.5, 0, 0);
$vbox->pack_start($align, FALSE, FALSE, 5);

# Create the Gtk2::ProgressBar and attach it to the window reference.
$pbar = Gtk2::ProgressBar->new;
$window->{pbar} = $pbar;

# Add a timer callback to update the value of the progress bar
$pbar->{timer} = Glib::Timeout->add(100, \&progress_timeout, $pbar);

$separator = Gtk2::HSeparator->new;
$vbox->pack_start($separator, FALSE, FALSE, 0);

# rows, columns, homogeneous
$table = Gtk2::Table->new(2, 3, FALSE);
$vbox->pack_start($table, FALSE, TRUE, 0);

# Add a check button to select displaying of the trough text
$check = Gtk2::CheckButton->new_with_label("Show Text");
$table->attach($check, 0, 1, 0, 1,
		['expand', 'fill'],
		['expand', 'fill'],
		5, 5);
$check->signal_connect(clicked => sub {
		my ($check, $progress) = @_;
		if ($progress->get_text)
			$progress->set_text('some text');
	}, $pbar);

# Add a check button to toggle activity mode
$pbar->{activity_mode} = 0;
$check = Gtk2::CheckButton->new_with_label("Activity Mode");
$table->attach($check, 0, 1, 1, 2,
		['expand', 'fill'],
		['expand', 'fill'],
		5, 5);
$check->signal_connect(clicked => sub {
		my ($check, $progress) = @_;
		$progress->{activity_mode} = not $progress->{activity_mode};
		if ($progress->{activity_mode})
	}, $pbar);

# Add a check button to toggle orientation
$check = Gtk2::CheckButton->new_with_label("Right to Left");
$table->attach($check, 0, 1, 2, 3,
		['expand', 'fill'],
		['expand', 'fill'],
		5, 5);
$check->signal_connect(clicked => sub {
		my ($check, $progress) = @_;
		$orientation = $progress->get_orientation;
		if    ('left-to-right' eq $orientation)
		elsif ('right-to-left' eq $orientation)
	}, $pbar);

# Add a button to exit the program.
$button = Gtk2::Button->new("Close");
$button->signal_connect_swapped(clicked => sub { $_[0]->destroy; }, $window);
$vbox->pack_start($button, FALSE, FALSE, 0);

# This makes it so the button is the default.

# This grabs this button to be the default button. Simply hitting the "Enter"
# key will cause this button to activate.