Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embed progressbar in a listbox using perl/tk

Status
Not open for further replies.

repstosd

Programmer
Mar 3, 2005
3
US
Hello, I would like to embed a progress bar into a listbox. Kinda like having a list of job names with progress bars next to them showing how much each job has completed. Make sense? Thanks!
 
Code:
#####
##### PROGRESS WINDOW
#####
     $main::amt_done = 0;
     $main::percent_complete = 0;
     $main::datafile = "";
     $main::mw = MainWindow->new;
     $main::mw->geometry("${main::wmwidth}x80+$main::wmpos");
     $main::mw->minsize(${main::wmwidth},'80');
     $main::mw->maxsize(${main::wmwidth},'80');
     $main::mw->title("$main::ProgName [Status]");
     $main::r0 = $main::mw->Frame;
     $main::r0->pack(-side => 'top');
     $main::r1 = $main::mw->Frame;
     $main::r1->pack(-side => 'top');
     $main::r2 = $main::mw->Frame;
     $main::r2->pack(-side => 'top');
     $main::r3 = $main::mw->Frame;
     $main::r3->pack(-side => 'top');

     $main::r0->Label(-width => '80', -text, &Win32::GetCwd(), -font => "$main::tfont")->pack(-side => 'left');

     $main::r1->Label(-width => '40', -text, "Files Processed", -font => "$main::tfont")->pack(-side => 'left');
     $main::r1->Label(-width => '1', -text, '%', -font => "$main::tfont")->pack(-side => 'right');
     $main::r1->Label(-width => '5', -textvariable, \$main::percent_complete, -font => "$main::tfont")->pack(-side => 'right');
     $main::progress1 = $main::r1->ProgressBar(
                       -width => 20,
                       -borderwidth => 2,
	                 -padx => 2,
	                 -pady => 2,
                       -relief => 'sunken',
                       -from => 0,
                       -gap => 0,
                       -to => 100,
                       -blocks => 100,
	                 -colors => [0 => 'blue', 25 => 'green' , 50 => 'yellow', => 75 => 'red'],
                       -value => $main::percent_complete,
                       -variable => \$main::percent_complete
     )->pack(-side => 'right', -fill => 'x');

     $main::fne = $main::r2->Entry(-width => '40', -text => \$main::datafile, -font => "$main::tfont")->pack(-side => 'left');
     $main::r2->Label(-width => '1', -text, '%', -font => "$main::tfont")->pack(-side => 'right');
     $main::r2->Label(-width => '5', -textvariable, \$main::amt_done, -font => "$main::tfont")->pack(-side => 'right');
     $main::progress2 = $main::r2->ProgressBar(
                       -width => 20,
                       -borderwidth => 2,
	                 -padx => 2,
	                 -pady => 2,
                       -relief => 'sunken',
                       -from => 0,
                       -gap => 0,
                       -to => 100,
                       -blocks => 100,
	                 -colors => [0 => 'blue', 25 => 'green' , 50 => 'yellow', => 75 => 'red'],
                       -value => $main::amt_done,
                       -variable => \$main::amt_done
     )->pack(-side => 'right', -fill => 'x');

     $main::r3label = $main::r3->Label(-width => '40', -text, "Press the \"s\" key to begin.", -font => "$main::tfont")->pack(-side => 'left');

     $main::mw->bind('<Control-c>' => sub{print STDOUT "Cancelled program.\n";writeln("ERR", "Cancelled program at ", &timestamp, "\n");exit(0);});
     $main::mw->bind('<KeyPress-s>' => [\&mainblock]);

     MainLoop; ##############

The 2nd to last line of code where you see &mainblock is a subroutine that runs the actual process where the progress bar just displays the progress.

There are updates to the window within the subroutine call. Sample below:

Code:
     $main::amt_done = 0;
     $main::fne->configure(-text => \$main::datafile);
     $main::mw->update();

Another sample:
Code:
          ######
          ###### PROGRESS
          ######
          $main::amt_done = sprintf("%6.2f", ((($main::placeholder / $main::eofpos) * 100)));
          if ($main::amt_done <= 0) {
               $main::percent_complete = sprintf("%6.2f", (($main::file_count/(scalar(@main::logfiles))) * 100));
          } else {
               $main::percent_complete = sprintf("%6.2f", ((($main::file_count/(scalar(@main::logfiles))) * 100) + ((1 / ((100/$main::amt_done) * scalar(@main::logfiles))) * 100)));
          }
          $main::mw->update();

The key is the $main::mw->update() which tells the program to update the progress bar window.


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top