tk/tcl 2 中的列表框
Listbox in tk/tcl 2
#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
如何获取所选变量的 .label1 值,而不是变量名?
即:代替“$ Var1”得到“123”;而不是“$ Var2”得到“456”;而不是“$ Var3”得到“789”
你需要改变这个:
.lstb1 insert 0 Var1 Var2 Var3
对此:
.lstb1 insert 0 $var1 $var2 $var3
还有这个:
set activeItem [$listbox get active]
对此:
set activeItem [$listbox get [$listbox curselection]]
最后是这个:
$label configure -text $$activeItem
对此:
$label configure -text $activeItem
(为什么 $listbox get active
不起作用?当您 select 列表框中的项目时,$listbox get active
会为您提供之前处于活动状态的项目 你点击了。如果你 select 两次相同的项目,$listbox get active
将第二次指出该项目。)
更新回复评论:
离开这一行
.lstb1 insert 0 Var1 Var2 Var3
原样,并更改此行:
set activeItem [$listbox get active]
至
set activeItem [lindex {123 456 789} [$listbox curselection]]
或
set values {123 456 789}
set activeItem [lindex $values [$listbox curselection]]
使用标签小部件的 -textvariable
选项如何?
proc SelectionHappened {listbox label} {
set varName [$listbox get [$listbox curselection]]
$label configure -textvariable $varName
}
#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
如何获取所选变量的 .label1 值,而不是变量名?
即:代替“$ Var1”得到“123”;而不是“$ Var2”得到“456”;而不是“$ Var3”得到“789”
你需要改变这个:
.lstb1 insert 0 Var1 Var2 Var3
对此:
.lstb1 insert 0 $var1 $var2 $var3
还有这个:
set activeItem [$listbox get active]
对此:
set activeItem [$listbox get [$listbox curselection]]
最后是这个:
$label configure -text $$activeItem
对此:
$label configure -text $activeItem
(为什么 $listbox get active
不起作用?当您 select 列表框中的项目时,$listbox get active
会为您提供之前处于活动状态的项目 你点击了。如果你 select 两次相同的项目,$listbox get active
将第二次指出该项目。)
更新回复评论:
离开这一行
.lstb1 insert 0 Var1 Var2 Var3
原样,并更改此行:
set activeItem [$listbox get active]
至
set activeItem [lindex {123 456 789} [$listbox curselection]]
或
set values {123 456 789}
set activeItem [lindex $values [$listbox curselection]]
使用标签小部件的 -textvariable
选项如何?
proc SelectionHappened {listbox label} {
set varName [$listbox get [$listbox curselection]]
$label configure -textvariable $varName
}