Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Best Way to Implement a Progress Bar (Tk?)

by tinman (Curate)
on Apr 11, 2001 at 03:33 UTC ( [id://71545]=note: print w/replies, xml ) Need Help??


in reply to Best Way to Implement a Progress Bar (Tk?)

I would recommend that you try Tk::ProgressBar by Graham Barr.. I've used it, its quite easy to use, and pretty flexible. My copy of widget examples (widget.bat in ActivePerl) has a full progress bar example.

As noted in the man pages, binding a variable {in the example, its called $percent_done} automagically updates the status bar too..
HTH
Update: fixed link.Thanks to Chmrr.

  • Comment on Re: Best Way to Implement a Progress Bar (Tk?)

Replies are listed 'Best First'.
Re: Re: Best Way to Implement a Progress Bar (Tk?)
by JojoLinkyBob (Scribe) on Apr 11, 2001 at 19:57 UTC
    Thanks for your reply. Actually, I did start out with that as an example, which maybe I should have stayed with. However, I still have the problem of not being able to update the variable continuously, because I need MainLoop to manage the GUI. Desert coder
Re: Re: Best Way to Implement a Progress Bar (Tk?)
by JojoLinkyBob (Scribe) on Apr 11, 2001 at 20:00 UTC
    Oh, do you have a link to your example? Maybe I should look at that first :) Thx, Desert coder

      If you want a way to update the status bar, Tk::after might be what you need.

      It allows time delayed callbacks to be made to a particular subroutine. So, you could have a subroutine that checks progress, and updates the status bar variable. This would be called by the $w->after command. (you can set a reference to the callback and the delay in milliseconds between calls.

      The widget example that I referred to is found in the standard distribution. Just in case you can't get your hands on a copy, I'm pasting it in here. Unfortunately, all my scripts that use Tk::ProgressBar are back at home :o) several thousand miles away...

      # ProgressBar - display various progress bars. use strict; use Tk; use Tk::ProgressBar; use Tk::Scale; my $mw = MainWindow->new; my $status_var = 0; my($fromv,$tov) = (0,100); foreach my $loop (0..1) { my $res = 0; my $blks = 10; my @p = qw(top bottom left right); foreach my $dir (qw(n s w e)) { $mw->ProgressBar( -borderwidth => 2, -relief => 'sunken', -width => 20, -padx => 2, -pady => 2, -variable => \$status_var, -colors => [0 => 'green', 50 => 'yellow' , 80 => 'red'], -resolution => $res, -blocks => $blks, -anchor => $dir, -from => $fromv, -to => $tov )->pack( -padx => 10, -pady => 10, -side => pop(@p), -fill => 'both', -expand => 1 ); $blks = abs($blks - ($res * 2)); $res = abs(5 - $res); } ($fromv,$tov) = ($tov,$fromv); } $mw->Scale(-from => 0, -to => 100, -variable => \$status_var)->pack; MainLoop;

      What I'd suggest is that you have a method that updates the variable $status_var in a callback.
      ie:

      # Your script should have $MainWindow->after(10,\&update_var); # this is the update variable callback. sub update_var { $status_var = $work_done/$total; $status_var = $status_var * 100; # get percentage }

      HTH

      widget is a program which is included in every Tk install. As cited above, if you are running Activestate Perl, you will find it as widget.bat in your perl\bin directory. *NIX users will find it in /usr/bin or suchlike -- wherever perl is to be found.

      FYI, however, here is a working ProgressBar example:

      #!/usr/bin/perl -w use strict; use Tk; use Tk::ProgressBar; use vars qw($status_var); my $main = Tk::MainWindow->new; $main->title('Foo'); $main->Label(-text=>'Loading, please wait...')->pack; $status_var = 0; $main->ProgressBar( -variable => \$status_var, -from => 0, -to => 100 )->pack; $main->after(500,\&do_stuff); Tk::MainLoop; sub do_stuff { for (1..10) { $status_var+= 10; $main->update; sleep 1; } Tk::exit; }

       
      perl -e 'print "I love $^X$\"$]!$/"#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://71545]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found