Reputation: 57
#Variables:
set var1 123
set var2 456
set var3 789
#Widgets:
label .label1 -text ""
listbox .lstb1 -height 3 -width 20 -selectmode browse
.lstb1 insert 0 Var1 Var2 Var3
#Procedure
proc SelectionHappened {listbox label} {
set activeItem [$listbox get active]
$label configure -text $$activeItem
}
#Interface
bind .lstb1 <<ListboxSelect>> {SelectionHappened .lstb1 .label1}
grid .label1 .lstb1 -sticky news
How to get in .label1 value of the selected variable, not the variable name ?
ie: instead of "$ Var1" get "123"; instead of "$ Var2" get "456"; instead of "$ Var3" get "789"
Upvotes: 0
Views: 1125
Reputation: 13252
You need to change this:
.lstb1 insert 0 Var1 Var2 Var3
to this:
.lstb1 insert 0 $var1 $var2 $var3
and this:
set activeItem [$listbox get active]
to this:
set activeItem [$listbox get [$listbox curselection]]
and finally this:
$label configure -text $$activeItem
to this:
$label configure -text $activeItem
(Why doesn't $listbox get active
work? When you select an item in the listbox, $listbox get active
gives you the item that was active before you clicked. If you select the same item twice, $listbox get active
will point out that item the second time.)
Update in response to comment:
Leave this line
.lstb1 insert 0 Var1 Var2 Var3
as is, and change this line:
set activeItem [$listbox get active]
to
set activeItem [lindex {123 456 789} [$listbox curselection]]
or
set values {123 456 789}
set activeItem [lindex $values [$listbox curselection]]
Documentation: label, lindex, listbox, set
Upvotes: 2
Reputation: 66
How about using the label widget's -textvariable
option?
proc SelectionHappened {listbox label} {
set varName [$listbox get [$listbox curselection]]
$label configure -textvariable $varName
}
Upvotes: 0