Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

a Tree that can fold and unfold

by llancet (Friar)
on Sep 19, 2008 at 09:09 UTC ( [id://712482]=perlquestion: print w/replies, xml ) Need Help??

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

Is there any widget in Tk that fulfills a tree that can fold and unfold its branches, like which we usually use? I have tried Tk::Tree and Tk::HList, but they don't have that feature. Futhermore, I'm not going to show a directory tree, so I cannot use the Tk::DirTree.

Replies are listed 'Best First'.
Re: a Tree that can fold and unfold
by GrandFather (Saint) on Sep 19, 2008 at 09:37 UTC

    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
      thanks a lot!

        Happy to help. It was a while ago that I first used Tk::Tree, but I seem to remember it took some time to figure out what the trick was!


        Perl reduces RSI - it saves typing
Re: a Tree that can fold and unfold
by zentara (Archbishop) on Sep 19, 2008 at 12:35 UTC
    If you like the look of Tk::CanvasDirTree, feel free to modify it. There is just a few code lines that add directories, you could change them to whatever. For your information, I made Tk::DirTree to emulate the Gtk2 Treeview's cool tree animation. You might want to look at Perl/Gtk2, here is an example. The Gtk2 methods are a bit trickier than Tk's.
    #! /usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; #standard window creation, placement, and signal connecting my $window = Gtk2::Window->new('toplevel'); $window->signal_connect('delete_event' => sub { Gtk2->main_quit; }); $window->set_border_width(5); $window->set_position('center_always'); #this vbox will geturn the bulk of the gui my $vbox = &ret_vbox(); #add and show the vbox $window->add($vbox); $window->show(); #our main event-loop Gtk2->main(); sub ret_vbox { my $vbox = Gtk2::VBox->new(FALSE,5); #create a scrolled window that will host the treeview my $sw = Gtk2::ScrolledWindow->new (undef, undef); $sw->set_shadow_type ('etched-out'); $sw->set_policy ('automatic', 'automatic'); #This is a method of the Gtk2::Widget class,it will force a mi +nimum #size on the widget. Handy to give intitial size to a #Gtk2::ScrolledWindow class object $sw->set_size_request (300, 300); #method of Gtk2::Container $sw->set_border_width(5); #this is one of the provided base Gtk2::TreeModel classes. my $tree_store = Gtk2::TreeStore->new(qw/Glib::String/); #fill it with arbitry data foreach my $parent_nr (1..30) { #the iter is a pointer in the treestore. We #use to add data. my $iter = $tree_store->append(undef); $tree_store->set ($iter,0 => "Parent $parent_nr"); foreach my $child (1..3){ #here we append child iters to the parent iter #and add data to those chils iters. my $iter_child = $tree_store->append($iter); $tree_store->set ($iter_child,0 => "Child $child of Pare +nt $parent_nr"); foreach my $gchild (1..3){ #here we append child iters to the parent iter #and add data to those chils iters. my $iter_child = $tree_store->append($ +iter_child); $tree_store->set ($iter_child,0 => "GrandC +hild $gchild of Parent $parent_nr"); } } } #this will create a treeview, specify $tree_store as its model my $tree_view = Gtk2::TreeView->new($tree_store); #create a Gtk2::TreeViewColumn to add to $tree_view my $tree_column = Gtk2::TreeViewColumn->new(); $tree_column->set_title ("Click to sort"); #create a renderer that will be used to display info #in the model my $renderer = Gtk2::CellRendererText->new; #add this renderer to $tree_column. This works like a Gtk2::Hb +ox # so you can add more than one renderer to $tree_column + $tree_column->pack_start ($renderer, FALSE); # set the cell "text" attribute to column 0 #- retrieve text from that column in treestore # Thus, the "text" attribute's value will depend on the row's + value # of column 0 in the model($treestore), # and this will be displayed by $renderer, # which is a text renderer $tree_column->add_attribute($renderer, text => 0); #add $tree_column to the treeview $tree_view->append_column ($tree_column); # make it searchable $tree_view->set_search_column(0); # Allow sorting on the column $tree_column->set_sort_column_id(0); # Allow drag and drop reordering of rows $tree_view->set_reorderable(TRUE); $sw->add($tree_view); $vbox->pack_start($sw,TRUE,TRUE,0); $vbox->show_all(); return $vbox; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: a Tree that can fold and unfold
by parv (Parson) on Sep 19, 2008 at 09:36 UTC
Re: a Tree that can fold and unfold
by vkon (Curate) on Sep 19, 2008 at 11:10 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://712482]
Approved by Corion
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found