Tree(3pm)

🌳 Tree(3pm) β€” the Tk::Tree manual, but make it make sense πŸ’…

Okay bestie, sit down, hydrate, because we're about to get into Tk::Tree β€” yes, the Perl/Tk widget that draws trees, and yes, it's giving main character energy. This is the Tree(3pm) page from the User Contributed Perl Documentation, rewritten so your brain actually absorbs it. No cap. 🫢


✨ NAME ✨

Tk::Tree β€” Create and manipulate Tree widgets. That's it, that's the whole vibe. 🌸


πŸŽ€ SYNOPSIS πŸŽ€

use Tk::Tree;

$tree = $parent->Tree(?options?);

Look at that. Iconic. ?options? is literally written like that, so don't come for me. 😌


πŸ‘‘ SUPER-CLASS πŸ‘‘

The Tree class is derived from the HList class and inherits all the methods, options and subwidgets of its super-class. So everything HList can do, Tree can do too β€” nepotism but make it functional. πŸ’€

Also important: a Tree widget is not scrolled by default. Noted. Write that down. πŸ“


🌈 STANDARD OPTIONS 🌈

Tree supports all the standard options of an HList widget. Head over to Tk::options for the full tea on the standard options. 🍡


πŸ’– WIDGET-SPECIFIC OPTIONS πŸ’–

These are the options that are so Tree, so specific, so special. Each one comes with a name, a class and a switch, and we're listing every single one because we don't skip. Periodt.

  • Name: browseCmd Class: BrowseCmd Switch: -browsecmd β€” Specifies a callback to call whenever the user browses on an entry (usually by single-clicking on the entry). The callback is called with one argument, the pathname of the entry. One click, one callback, one pathname. ✨
  • Name: closeCmd Class: CloseCmd Switch: -closecmd β€” Specifies a callback to call whenever an entry needs to be closed (See "BINDINGS" below). This method is called with one argument, the pathname of the entry. This method should perform appropriate actions to close the specified entry. If the -closecmd option is not specified, the default closing action is to hide all child entries of the specified entry. So like, if you don't bring your own closing behaviour, it just ghosts the children. πŸ™ˆ
  • Name: command Class: Command Switch: -command β€” Specifies a callback to call whenever the user activates an entry (usually by double-clicking on the entry). The callback is called with one argument, the pathname of the entry. Double-click β†’ callback β†’ pathname. Simple as. πŸ’…
  • Name: ignoreInvoke Class: IgnoreInvoke Switch: -ignoreinvoke β€” A Boolean value that specifies when a branch should be opened or closed. A branch will always be opened or closed when the user presses the (+) and (-) indicators. However, when the user invokes a branch (by doublc-clicking or pressing <Return>), the branch will be opened or closed only if -ignoreinvoke is set to false (the default setting). Yes, "doublc-clicking" β€” that's the original spelling, we keep it, we honor it, we do not correct it. 🫑
  • Name: openCmd Class: OpenCmd Switch: -opencmd β€” Specifies a callback to call whenever an entry needs to be opened (See "BINDINGS" below). This method is called with one argument, the pathname of the entry. This method should perform appropriate actions to open the specified entry. If the -opencmd option is not specified, the default opening action is to show all the child entries of the specified entry. Bringing the babies out of hiding. πŸ¦‹

🌸 DESCRIPTION 🌸

The Tree method creates a new window and makes it into a Tree widget and return a reference to it. Additional options, described above, may be specified on the command line or in the option database to configure aspects of the Tree widget such as its cursor and relief. Cursor and relief, the accessories of widgets. πŸ’…

The Tree widget can be used to display hierarchical data in a tree form. The user can adjust the view of the tree by opening or closing parts of the tree. It's literally a tree. You open branches, you close branches, you feel powerful. 🌳

static trees, the low-effort way 🧚

To display a static tree structure, you can add the entries into the Tree widget and hide any entries as desired. Then you can call the autosetmode method. This will set up the Tree widget so that it handles all the open and close events automatically. (the demonstration program Tixish/examples/perl-tix-tree).

dynamic trees, the high-effort way πŸ”₯

The above method is not applicable if you want to maintain a dynamic tree structure, i.e, you do not know all the entries in the tree and you need to add or delete entries subsequently. To do this, you should first create the entries in the Tree widget. Then, use the setmode method to indicate the entries that can be opened or closed, and use the -opencmd and -closecmd options to handle the opening and closing events. (Please see the demonstration program Tixish/examples/perl-tix-dyntree).

want it scrolled? πŸŽ€

Use either

$parent->Scrolled('Tree', ... );

or

$parent->ScrlTree( ... );

to create a scrolled Tree. See Tk::Scrolled for details. Two paths, same slay. ✨


πŸ’œ WIDGET METHODS πŸ’œ

The Tree method creates a widget object. This object supports the configure and cget methods described in Tk::options which can be used to enquire and modify the options described above. The widget also inherits all the methods provided by the generic Tk::Widget class. Inheritance really said "I got you bestie." 🫢

The following additional methods are available for Tree widgets:

$tree->add_pathimage(treeRegExp [, openImg, closeImg])

This method defines images for a given path (images must be in xpm format). The path can be determined by a simplified regular expression. There are just three metasymbols:

  • ^ β€” at the beginning of the treeRegExp, same as in Perl regular expressions
  • * β€” anything
  • $ β€” at the end of the TreeRegExp, the same as in Perl regular expressions

Examples:

$tree->add_pathimage('^root','openfolder','folder');

matches root, root.foo, root.bar, but not foo.root

$tree->add_pathimage('root.*.class','openfolder','folder');

matches all paths containing root.<anything>.class, but not root.<anything>.<anything>.class β€” * is one part of the path. If you want to use a wildcard for two steps, you have to use *.*. ⚠️ one star = one step, do not come crying to me.

$tree->add_pathimage('class$','openfolder','folder');

This matches all path with class at the end. πŸ’…

$tree->autosetmode

This method calls the setmode method for all the entries in this Tree widget: if an entry has no child entries, its mode is set to none. Otherwise, if the entry has any hidden child entries, its mode is set to open; otherwise its mode is set to close. It's like a vibe check for every single entry. ✨

$tree->child_entries([$path][,$depth])

This method returns in list context an array that contains all pathnames of subentries within the given path. In scalar context it returns the number of subentries in the given path. Context really changes everything. 😭

Example:
  root
   | foo
   | bar
   |  | bar1
   |  | bar2

my @childentries = $tree->child_entries('root.bar');
# returns (root.bar.bar1, root.bar.bar2)

my $nr_of_subentries = $tree->child_entries('root',2);
# returns 4

If $path is omitted, all it is assumed, that the entry above root is meant. $depth defines the numbers of levels. 🧚

$tree->close(entryPath)

Close the entry given by entryPath if its mode is close. Shut the door bestie. πŸšͺ

$tree->getmode(entryPath)

Returns the current mode of the entry given by entryPath. Just peeking. πŸ‘€

$tree->open(entryPath)

Open the entry given by entryPath if its mode is open. Let them in! πŸŽ€

$tree->setmode(entryPath, mode)

This method is used to indicate whether the entry given by entryPath has children entries and whether the children are visible. mode must be one of open, close or none.

  • If mode is set to open, a (+) indicator is drawn next to the entry.
  • If mode is set to close, a (-) indicator is drawn next to the entry.
  • If mode is set to none, no indicators will be drawn for this entry.
  • The default mode is none.

The open mode indicates the entry has hidden children and this entry can be opened by the user. The close mode indicates that all the children of the entry are now visible and the entry can be closed by the user. ✨ plus/minus indicators = the little accessories of the tree world. πŸ’…


⭐ BINDINGS ⭐

The basic mouse and keyboard bindings of the Tree widget are the same as the bindings of the HList widget. In addition, the entries can be opened or closed under the following conditions:

  1. If the mode of the entry is open, it can be opened by clicking on its (+) indicator.
  2. If the mode of the entry is close, it can be closed by clicking on its (-) indicator.

That's it. Click the little symbol, get the little result. Iconic. πŸ”₯


πŸ¦‹ SEE ALSO πŸ¦‹

Tk::HList β€” go read it, it's the parent, respect your elders. πŸ’œ


πŸ“ AUTHOR πŸ“

Perl/TK version by Chris Dean <ctdean@cogit.com>. Original Tcl/Tix version by Ioi Kim Lam.

Additions by Renee Baecker <module@renee-baecker.de>

Shoutout to the whole lineage honestly. πŸ‘‘


🌈 ACKNOWLEDGEMENTS 🌈

Thanks to Achim Bohnet <ach@mpe.mpg.de> for all his help. We love a helpful legend. 🫢


And that's the page, bestie β€” perl v5.40.1, dated 2025-04-13, and this was Tree(3pm). Go forth and open some branches. πŸŒ³βœ¨πŸ’–

πŸ“¦ where this page comes from