Hi peterbe,

Your best source of information on this is going to be the perldoc pages for Tk::Tree. Here is an example of how I create the browse trees collapsed.

First start by creating your Tree (in this case I want it in a "Scrolled" widget):

$list = $browser->Scrolled( 'Tree', '-relief' => 'sunken', '-border' => '2', '-width' => '80', '-height' => '30', '-scrollbars' => 'osoe', '-command' => \&edit )->pack( '-side' => 'left', '-fill' => 'both', '-expand' => '1' );
With your Tree widget created there are now three basic types of leaves on it - Primary ones that are parents only, Secondary that are parents and are also childern, and Tertiary that are children but are not parents. You can have multiple levels of the secondary ones.

To create your primary leaves so that they are visible, but have all their children collapsed below them do this:

$list->add("rec$i", '-text' => "($i) $displayLine"); $list->setmode("rec$i", 'open'); $list->close("rec$i");
This creates the leaf, sets it to be "open-able" and then collapses the child leaves underneath. The secondary are created like this:
$list->add("rec$i.$j", '-text' => "$segments[$j]"); $list->setmode("rec$i.$j", 'open'); $list->close("rec$i.$j"); $list->hide('entry', "rec$i.$j");
This is close to what we do for the primary - except because its parent is closed we then hide the leaf. The tertiary are as follows:
$list->add("rec$i.$j.$k", '-text' => "$fields[$k]"); $list->hide('entry', "rec$i.$j.$k");
This just creates the leaf and hides it - it has no children of its own, so we are done.

To see a working example of this you can take a look at my HL7 Browser which uses Tree for a living. This example was taken out of that code and cleaned up a bit for clarity. Figuring this out was a big pain for me, so I hope this helps you. :)

Good luck,
{NULE}
--
http://www.nule.org


In reply to Re: Tk::Tree - changing default behavior by {NULE}
in thread Tk::Tree - changing default behavior by peterbe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.