Chapter 40. Displaying Databases with the Tree Widget

Steve Lidie

This article discusses Perl/Tk’s Tk::Tree widget, which displays hierarchical data in a tree format. In computer science, a tree is a data structure that starts at a root node and branches to other nodes, which can be internal nodes or leaf nodes at the bottom of the tree. An internal node has one or more child nodes; it’s called the parent of those nodes. (Any child node at the end of a branch is a leaf node, since it has no children.) Nodes sharing the same parent are siblings. Unlike a physical tree, the root node is usually shown at the top, and leaves at the bottom of the structure. See Figure 40-1.

A sample tree
Figure 40-1. A sample tree

In contrast, Tk::Tree draws “sideways” trees, with the root node at the top left and branches growing down and to the right. While Tk::Tree is in the standard Perl/Tk distribution, there’s another tree widget on CPAN: Tk::TreeGraph, by Dominique Dumont. I won’t describe it here, but Figure 40-2 shows it, and you can read about it on the Perl/Tk modules page http://www.Lehigh.EDU/~sol0/ddumont/ptk_module_list.html.

Tk::Tree is derived from Tk::HList, the hierarchical list widget. From a programming point of view, Tree is much simpler to use because it masks the complexity of HList and automatically adds open and close buttons, thus exposing a much simpler interface. Figure 40-3 (interestingly enough, ...

Get Web, Graphics & Perl/Tk Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.