3 # This demonstration script creates a toplevel window containing a Ttk
4 # tree widget configured as a multi-column listbox.
6 # RCS: @(#) $Id: mclist.tcl,v 1.3 2007/12/13 15:27:07 dgp Exp $
8 if {![info exists widgetDemo]} {
9 error "This script should be run from the \"widget\" demo."
18 wm title $w "Multi-Column List"
19 wm iconname $w "mclist"
23 ttk::label $w.msg -font $font -wraplength 4i -justify left -anchor n -padding {10 2 10 6} -text "Ttk is the new Tk themed widget set. One of the widgets it includes is a tree widget, which can be configured to display multiple columns of informational data without displaying the tree itself. This is a simple way to build a listbox that has multiple columns. Clicking on the heading for a column will sort the data by that column. You can also change the width of the columns by dragging the boundary between them."
27 pack [addSeeDismiss $w.seeDismiss $w] -side bottom -fill x
29 ttk::frame $w.container
30 ttk::treeview $w.tree -columns {country capital currency} -show headings \
31 -yscroll "$w.vsb set" -xscroll "$w.hsb set"
32 if {[tk windowingsystem] ne "aqua"} {
33 ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
34 ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
36 scrollbar $w.vsb -orient vertical -command "$w.tree yview"
37 scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
39 pack $w.container -fill both -expand 1
40 grid $w.tree $w.vsb -in $w.container -sticky nsew
41 grid $w.hsb -in $w.container -sticky nsew
42 grid column $w.container 0 -weight 1
43 grid row $w.container 0 -weight 1
45 ## The data we're going to insert
47 Argentina {Buenos Aires} ARS
48 Australia Canberra AUD
57 Mexico {Mexico City} MXN
59 {South Africa} Pretoria ZAR
60 {United Kingdom} London GBP
61 {United States} {Washington, D.C.} USD
64 ## Code to insert the data nicely
65 set font [ttk::style lookup [$w.tree cget -style] -font]
66 foreach col {country capital currency} name {Country Capital Currency} {
67 $w.tree heading $col -command [list SortBy $w.tree $col 0] -text $name
68 $w.tree column $col -width [font measure $font $name]
70 foreach {country capital currency} $data {
71 $w.tree insert {} end -values [list $country $capital $currency]
72 foreach col {country capital currency} {
73 set len [font measure $font "[set $col] "]
74 if {[$w.tree column $col -width] < $len} {
75 $w.tree column $col -width $len
80 ## Code to do the sorting of the tree contents when clicked on
81 proc SortBy {tree col direction} {
82 # Build something we can sort
84 foreach row [$tree children {}] {
85 lappend data [list [$tree set $row $col] $row]
88 set dir [expr {$direction ? "-decreasing" : "-increasing"}]
91 # Now reshuffle the rows into the sorted order
92 foreach info [lsort -dictionary -index 0 $dir $data] {
93 $tree move [lindex $info 1] {} [incr r]
96 # Switch the heading so that it will sort in the opposite direction
97 $tree heading $col -command [list SortBy $tree $col [expr {!$direction}]]