d1d3f471f9b43f9c858b688e7b297fcc8f62301a
3 # This demonstration script creates a toplevel window containing a Ttk
4 # tree widget configured as a multi-column listbox.
6 if {![info exists widgetDemo
]} {
7 error "This script should be run from the \"widget\" demo."
16 wm title
$w "Multi-Column List"
17 wm iconname
$w "mclist"
21 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."
25 pack [addSeeDismiss
$w.seeDismiss
$w] -side bottom
-fill x
27 ttk
::frame $w.container
28 ttk
::treeview $w.tree
-columns {country capital currency
} -show headings
\
29 -yscroll "$w.vsb set" -xscroll "$w.hsb set"
30 if {[tk windowingsystem
] ne
"aqua"} {
31 ttk
::scrollbar $w.vsb
-orient vertical
-command "$w.tree yview"
32 ttk
::scrollbar $w.hsb
-orient horizontal
-command "$w.tree xview"
34 scrollbar $w.vsb
-orient vertical
-command "$w.tree yview"
35 scrollbar $w.hsb
-orient horizontal
-command "$w.tree xview"
37 pack $w.container
-fill both
-expand 1
38 grid $w.tree
$w.vsb
-in $w.container
-sticky nsew
39 grid $w.hsb
-in $w.container
-sticky nsew
40 grid column
$w.container
0 -weight 1
41 grid row
$w.container
0 -weight 1
43 ## The data we're going to insert
45 Argentina
{Buenos Aires
} ARS
46 Australia Canberra AUD
55 Mexico
{Mexico City
} MXN
57 {South Africa
} Pretoria ZAR
58 {United Kingdom
} London GBP
59 {United States
} {Washington
, D.C.
} USD
62 ## Code to insert the data nicely
63 set font [ttk
::style lookup
[$w.tree cget
-style] -font]
64 foreach col
{country capital currency
} name
{Country Capital Currency
} {
65 $w.tree heading
$col -command [list SortBy
$w.tree
$col 0] -text $name
66 $w.tree column
$col -width [font measure
$font $name]
68 foreach {country capital currency
} $data {
69 $w.tree insert
{} end
-values [list $country $capital $currency]
70 foreach col
{country capital currency
} {
71 set len
[font measure
$font "[set $col] "]
72 if {[$w.tree column
$col -width] < $len} {
73 $w.tree column
$col -width $len
78 ## Code to do the sorting of the tree contents when clicked on
79 proc SortBy
{tree col direction
} {
80 # Build something we can sort
82 foreach row
[$tree children
{}] {
83 lappend data
[list [$tree set $row $col] $row]
86 set dir
[expr {$direction ?
"-decreasing" : "-increasing"}]
89 # Now reshuffle the rows into the sorted order
90 foreach info [lsort -dictionary -index 0 $dir $data] {
91 $tree move
[lindex $info 1] {} [incr r
]
94 # Switch the heading so that it will sort in the opposite direction
95 $tree heading
$col -command [list SortBy
$tree $col [expr {!$direction}]]