my $progressbar = $mw->ProgressBar( [ option => value ] );where some common options are listed 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.
Randy Kobes 2003-11-17