lyle2000 has asked for the wisdom of the Perl Monks concerning the following question:

I have written a perl script that uses the tree widget quite nicley. However I cannot get the tree to initially display with all the nodes closed. I have used the $tree->setmode($path, 'open') but to no avail. When I do this each node starts with the "+" sign but is still open. Is there a flag I am missing?

Replies are listed 'Best First'.
Re: Perl/TK trees always start open
by llancet (Friar) on Nov 05, 2009 at 01:56 UTC
    You have to manually write some code to recursively close all nodes. I've been not using Tk for some time, so I can only give you some pseudo-code:
    sub close_all { my $node = shift; foreach my $child ($node->method_to_get_its_children) { close_all($child); } $node->method_to_close_itself; }
Re: Perl/TK trees always start open
by zentara (Cardinal) on Nov 05, 2009 at 12:58 UTC
    i think all you need is $t->close('/') before the mainloop

    #!/usr/bin/perl use strict; use Tk; use Tk::Tree; my $mw = tkinit; my $t = $mw->Scrolled( 'Tree', -separator => '/', -scrollbars => 'osoe', )->pack; foreach (qw(/ /a /b /c /a/s /a/s/d /a/s/d/f /b/n /b/n/m)) { my @text = reverse split( /\//, $_ ); $t->add( $_, -text => @text[0] ); } $t->autosetmode; foreach ( $t->info( 'children', '/' ) ) { $t->close($_); } $mw->Button(-text=>'Close nodes', -command => sub{ closeTree($t, '/a') } )->pack(); $t->close('/'); MainLoop; 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; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Perl/TK trees always start open
by gmargo (Hermit) on Nov 04, 2009 at 22:24 UTC

    Can you provide some example code, please?

      They system I am writting on is on a isolated network I will see what I can do get a sample over.
Re: Perl/TK trees always start open
by lamprecht (Friar) on Nov 05, 2009 at 08:07 UTC
    Hi,
    $tree->hide( entry => 'entry.path' ); $tree->autosetmode;

    should work.


    Cheers, Christoph