ProgressBar

A ProgressBar widget is a widget which can be used to graphically monitor the progress of some process. A common example of this occurs in many graphical ftp clients, where when downloading or uploading a file a bar will appear, informing the user of the progress of the transfer. The basic sytax for creating such a widget is
my $progressbar = $mw->ProgressBar( [ option => value ] );
where some common options are listed below. An example of a ProgressBar appears below.
#!perl
# file progress.pl
use Tk;
use Tk::ProgressBar;
use strict;
my $percent_done = 0;
my $mw = MainWindow->new;
my $text = "Percentage to date: $percent_done";
$mw->title('Progress Bar');
my $message = $mw->Message(-textvariable => \$text,
                           -width => 130,
                           -border => 2);
my $progress = 
  $mw->ProgressBar(-width => 200,
                   -height => 20,
                   -from => 0,
                   -to => 100,
                   -anchor => 'e',
                   -blocks => 5,
                   -colors => [0, 'green', 50, 'yellow' , 80, 'red'],
                   -variable => \$percent_done,
                   );
my $exit = $mw->Button(-text => 'Exit',
                      -command => [$mw => 'destroy']);
my $get = $mw->Button(-text => 'Press to start!',
                      -command => \&start);
$get->pack;
$progress->pack;
$message->pack;
$exit->pack;
MainLoop;
sub start {
  for ($percent_done=0; $percent_done <= 100; $percent_done +=5) {
    $text = "Percentage to date: $percent_done";
    $mw->update;
    sleep 1;
  }
  $text = 'Done!';
  $mw->update;
}
In this example the progress bar is associated with the variable $percent_done, and as this variable changes, the bar will be filled in. This variable is also used in forming the $message widget to give a statement about the state of the overall progress. The $percent_done variable is changed through the invocation of the start subroutine, called when the $get button is pressed. In this routine $percent_done is changed within the loop, with a sleep statement inserted in order that we can see the progress occur. Note the use of the $mw->update call on the main window in order to update the window as the variable changes. A screen shot of this window appears below.

Figure 3.26: Example of a ProgressBar widget
Image progressbar

Randy Kobes 2003-11-17