Tree and HList both support that feature. Look at the documentation for hide and show methods in the documentation for HList, and for the -state => 'normal'. The trick however is that you must call setmode on each node in the tree to set its initial state.
use strict;
use warnings;
use Tk;
use Tk::Tree;
my $main = MainWindow->new(-title => "Demo" );
my $tree = $main->ScrlTree(
-font => 'FixedSys 8',
-itemtype => 'text',
-separator => '/',
-scrollbars => "se",
-selectmode => 'single',
);
$tree->pack( -fill => 'both', -expand => 1 );
$tree->add ('root', -text => 'root of all ...', -state => 'normal' );
$tree->add ('root/top 1', -text => 'first top level node', -state => '
+normal' );
$tree->add ('root/top 2', -text => 'second top level node', -state =>
+'normal' );
$tree->add ('root/top 2/nested 1', -text => 'first nested node', -stat
+e => 'normal' );
$tree->add ('root/top 2/nested 2', -text => 'second nested node', -sta
+te => 'normal' );
openTree ($tree);
MainLoop;
sub openTree {
my $tree = shift;
my ( $entryPath, $openChildren ) = @_;
my @children = $tree->info( children => $entryPath );
return if !@children;
for (@children) {
openTree( $tree, $_, 1 );
$tree->show( 'entry' => $_ ) if $openChildren;
}
$tree->setmode( $entryPath, 'close' ) if length $entryPath;
}
sub closeTree {
my $tree = shift;
my ( $entryPath, $hideChildren ) = @_;
my @children = $tree->info( children => $entryPath );
return if !@children;
for (@children) {
closeTree( $tree, $_, 1 );
$tree->hide( 'entry' => $_ ) if $hideChildren;
}
$tree->setmode( $entryPath, 'open' ) if length $entryPath;
}
Perl reduces RSI - it saves typing
|