Recent Changes - Search:




TreeView is a table/tree widget with powerful features. Here is a screen shot:


Following is a simple example to display a list of files:

 pack [treeview .t]
 foreach i [glob *] {
    .t insert end [list $i]

Note the use of [list] is required because TreeView uses list separators by default. This can be overriden with an explicit delimiter character as in the following example which displays files in a tree down to 2 directory levels:

 pack [treeview .t -autocreate 1 -separator /]
 foreach i [glob  */*] {
    .t insert end $i

Data Addressing

TreeView provides methods for updating data elements. It also supports accessing dict sub-elements using array notation:

 set t .t
 pack [treeview $t] -fill both -expand y
 $t column insert end X Y
 $t insert end A -data {X 1 Y 2}
 $t insert end B -data {X 3 Y "a 1 b 2"}

 $t entry incr 0->A X [$t entry get 0->B X]
 $t entry set 0->A Y 3
 $t entry incr 0->B Y(a) 9

External Data

A data tree can be attach to a TreeView, eg.

 set t [tree create]
 $t insert 0 -label Managers
 $t insert 0->Managers -label Tina -data {Age 29 Salary 200}
 pack [treeview .t -tree $t] -fill both -expand y
 eval .t col insert end [lsort [$t keys nonroot]]
 .t open all
 puts [$t incr 0->Managers->Tina Age]

Demand Loading

Data can be demand loaded into a treeview tree as it becomes visible or scrolls into view, eg.

pack [treeview .t] -fill both -expand y
set t [tree create]
foreach i {A B C} {
  .t col insert end $i -fillcmd [list FillMe $t $i]

proc FillMe {t col id} {
  return $col$id

$t populate 10000
.t conf -tree $t

One use for this is to load the rowids for an sqlite database table, and then loading data rows on demand.

See also Tree demand loading.

Automatics Styles

TreeView makes it easy to apply a style to given depth levels automatically. For example, the following applies lev1 to all toplevel nodes, and lev2 to all nodes of depth 2.

  .t style create textbox lev1 -bg LightBlue
  .t style create textbox lev2 -bg LightGreen
  .t conf -levelstyles {lev1 lev2}

Alternating row colors is another common effect used in tables. However, for trees the style has to be reapplied everytime a subtree of odd length is opened or closed. The following code snipet shows how TreeView does automatically in TreeView:

  .t style create textbox alt -bg LightBlue
  .t conf -altstyle alt

TreeView vs TTK

For basic usage, Blt TreeView provides a programming model that is similar to Ttk Treeview. However, TreeView privides much better support for auto-sizing data to fit the window. For example, the following compares TTK and TreeView:

Note how TTK is prone to clipping the text. Here is the code for above:

 proc TtkTree {t} {
   pack [ttk::treeview $t] -fill both -expand y
   $t conf -columns "X Y Z"
   foreach i {X Y Z} { $t heading $i -text $i }
   $t insert {} end -id A -text A -tags A -open 1
   $t insert A end -id a -text a -tags Aa -values {0 8 9}
   $t insert {} end -id B -text B -tags B -open 1
   $t insert B end -id b -text b -values [list "123456789\nabcdefhijklmnop"]
   $t insert {} end -id C -text C -values 99
   $t tag conf A -font "Verdana -30 bold" -background red
   $t tag conf B -background Blue -foreground White
   $t heading #0 -text TTK

 proc BltTree {t} {
   pack [blt::treeview $t] -fill both -expand y
   foreach i {X Y Z} { $t column insert end $i }
   $t insert end A -isopen 1 -font "Verdana -30 bold" -bg red
   $t insert end "A a" -data "X 0 Y 8 Z 9"
   $t insert end B -style B -isopen 1
   $t insert end "B b" -styles "X xb" -data [list X "123456789\nabcdefhijklmnop"]
   $t insert end C -data {X 99}
   $t style conf B -bg Blue -fg white
   $t style conf xb -bg pink
   $t column conf #0 -title BLT

 wm geom . 300x400
 eval BltTree .s
 eval TtkTree .t

Wize Data Trees

Wize supports an abbreviated way to define trees, eg.

  *tree new t = {
      = Age Salary
      Managers {
          = Age Salary Title
          Tina 29 10000 President
          Tom  28 8000 VP
      Staff {
          # Inherit the titles of parent ie. "Age Salary".
          Mary 10 6000
          Sam  10 6000
  pack [treeview .t -tree $t -width 600 -height 600] -fill both -expand y
  eval .t col insert end [lsort [$t keys nonroot]]
  .t open all
  puts [$t incr 0->Managers->Tina Age]

See Tree for more details.


Here is a list of TreeView features:

  • Auto-sizing column widths and row heights.
  • Hide/move columns or nodes.
  • Sortable by columns or sub-trees.
  • External data storage (in a blt::tree).
  • Multiple TreeViews can share all a tree.
  • Easy to use dynamic loading (for sub-trees).
  • Support for multiple style types, including:
    • textbox: text cell with optional images.
    • checkbox: a boolean value.
    • combobox: a multi-choice value.
    • barbox: numeric value with progress bar.
    • windowbox: arbitrary embedded windows.
  • Styles can be applied to cols, rows and/or cells
  • The -altstyle option for alternating rows (bgcolor, etc).
  • The -levelstyles option for per-level styles.
  • Background image-tile: widget, columns, and cell-styles.
  • Drop shadow text.
  • Powerful builtin cell editing.
  • Dual mode display: flat and tree.
Edit - History - Print - Recent Changes - Search
Page last modified on September 15, 2011, at 01:04 PM