blackadder has asked for the wisdom of the Perl Monks concerning the following question:

O holly ones,

I have a requirement to program a progress bar. I have done a bit of TK before, but I am a bit rusty and I haven't actually done a progress bar before. I had a look at the widgets.bat and http://www.perlmonks.org/index.pl?node_id=233973 but I was going into endless circles. Basically, what really help me is, if I can see an example that will count 1 to 100 and sleeps in-between counts so I can see the progress of the bar,..like below:
My @int = (1..100); For (@int) { do something or sleep(1); }
If I can represent the above in a progress bar,..then I can say I have learnt it. Thanks.

Replies are listed 'Best First'.
Re: TK Progress Bar
by bbfu (Curate) on Aug 06, 2003 at 18:00 UTC

    #!/usr/bin/winperl use strict; use warnings; use Tk; use Tk::ProgressBar; my $mw = MainWindow->new(); my $prog = $mw->ProgressBar( -length => 200, # Actually width -width => 20, # Actually height -gap => 0, -value => 0, -colors => [0, 'navy'], )->pack(-pady => 5, -padx => 5), my @ints = (1 .. 100); for my $int (@ints) { # Set the new value of the progress bar to # the old value ($prog->value) plus one. $prog->value($prog->value + 1); tksleep($mw, 10); } # Like sleep, but actually allows the display to be # updated, and takes milliseconds instead of seconds. sub tksleep { my $mw = shift; my $ms = shift; my $flag = 0; $mw->after($ms, sub { $flag++ }); $mw->waitVariable(\$flag); }

    bbfu
    Black flowers blossom
    Fearless on my breath

Re: TK Progress Bar
by zentara (Cardinal) on Aug 07, 2003 at 16:01 UTC
    There is also Tk::ProgressIndicator which may be a bit simpler.
    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::ProgressIndicator; my $MainWindow = MainWindow->new(); my $total = 100; # this will vary my $ProgressIndicator = $MainWindow->ProgressIndicator ( '-current' => 0, '-limit' => 100, # this is fixed! '-increment' => 1, '-height' => 20, '-width' => 400 )->pack; foreach my $offset (0..$total) { $ProgressIndicator->configure ('-current' => $offset*100/$total,) +; $MainWindow->update; } MainLoop;
      This is a gorgeous bit of code, I am sure I will be using it as much as the "use strict". Thanks a lot.