in reply to simple Perl/Tk problems

  1. Check out Tk::ProgressBar. It should be pretty straight-forward (the POD has basic example code). Be sure to use Tk->DoOneEvent() to keep the window responsive and update the display.
  2. You need to bind a handler for keypresses on the list, then use some combination of DirTree->open() (see Tk::Tree), DirTree->selectionSet(), and DirTree->see() (see Tk::HList).
  3. You should be able to set up one or two container widgets and then either pack things left or right, or use a grid layout instead.

Note: Perldoc.com doesn't seem to have any Tk documentation, and neither does search.cpan.org seem to include the documentation for Tk::bind, Tk::pack, nor Tk::grid. Just do `perldoc Tk::bind` (etc) for those docs.

bbfu
Black flowers blossum
Fearless on my breath
Teardrops on the fire
Fearless on my breath

Replies are listed 'Best First'.
Re: (bbfu) Re: simple Perl/Tk problems
by blackadder (Hermit) on Sep 11, 2002 at 10:45 UTC
    Well, below is the example from the k::Progress bar.
    Now, it’s NOT blatantly obvious on to how to get this code to work in conjunction with the Dirtree so that if a large drive was selected then the progress bar will indicate that the script is obtaining information and the progress bar will disappear once the data is ready to be displayed.
    Is there simpler example for a slow monk like me please?
    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(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;

      You're right, of course. It's not quite as straight-forward as I was thinking it would be. :)

      The problem is that you need a hook into the default handler for the dircmd callback, and it doesn't seem that you can get one. You're probably going to need to write your own. :(

      Here is the source of the default handler (taken directly from Tk/DirTree.pm):

      sub DirCmd { my( $w, $dir, $showhidden ) = @_; my $h = DirHandle->new( $dir ) or return(); my @names = grep( $_ ne '.' && $_ ne '..', $h->read ); @names = grep( ! /^[.]/, @names ) unless $showhidden; return( @names ); }

      Pretty simple. You just need to include a modified version of that sub in your program, and give your Tk::DirTree object a reference to it as the handler for the -dircmd callback (ie, $dirtree->configure(-dircmd => \&MyDirCmd);).

      In your copy, you need to break out that grep, probably into a for loop, or something, so that you can have your code update the Tk::ProgressBar. Of course, that's going to slow down reading of all directories. Not by much, though, hopefully.

      Anyway, regarding showing and hiding the progress bar... You could use a pop-up window to show it (there's even a Tk::ProgressBarDialog or something similar in the Snippets section of this site, I believe). I, however, would probably go with a status-bar type deal, where the progress bar is always shown, but empty, at the bottom of the window, and it just fills up while reading directories. *shrug* Up to you.

      Hope that helps!

      bbfu
      Black flowers blossum
      Fearless on my breath