Hi tame1,

A couple of things.

You only need to construct your progress bar once.  You then manage it by making changes to the variable (in your case, $percent_done).

I had never used Tk::ProgressBar before (I usually just construct my own), so I just tried it, and it's quite easy.

Here's an example that's pretty close to yours, except that it doesn't try to examine files from an array (like @SOURCE), but rather, for purposes of example, just changes the value of $percent_done:

#!/usr/bin/perl -w + use strict; use warnings; + use Tk; use Tk::ProgressBar; + my $percent_done = 0; + my $mw = new MainWindow(-title => 'Progress Bar Demo'); my $top = $mw->Frame()->pack(-expand => 1, -fill => 'both'); my $pb = $top->ProgressBar( -width => 20, -height => 200, -from => 0, -to => 100, -blocks => 10, -colors => [0, 'green', 50, 'yellow', 80, 'red'], -variable => \$percent_done ); $pb->pack(); $mw->after(100 => \&main_loop); MainLoop; + sub main_loop { for (my $i = 0; $i < 100; $i++) { $percent_done = $i; $mw->update(); select(undef, undef, undef, 0.1); } }

The delay loop is caused by the code select(undef, undef, undef, 0.1);.

It's important to issue an update to the main window with $mw->update();, otherwise you may never see the meter updating.

All you need to do is put your code in the subroutine main_loop (or whatever you change its name to), and make sure that you update the percentage variable $percent_done in a meangful way.  For example, if you're going by number of files processed $nprocessed, out of a total number of file $total, then $percent_done should be calculated something like:  $percent_done = 100 * $nprocessed / $total.

A final note:  In my experience, you may get a smoother progress meter if you go by total bytes rather than total files, especially in cases where some files are tiny, and some are huge.  The way to do this is make $total equal to the total bytecount (from all the files), and $nprocessed the running count of bytes processed.  Then the same formula should apply.

Good luck!


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: Tk::ProgressBar and system cp by liverpole
in thread Tk::ProgressBar and system cp by tame1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.