Tcl 单选按钮作为一个
我正在从数据库构建一个对话框,并使用键作为单选按钮的变量。每当我单击单选按钮时,同一列中的所有按钮都会更改。字典中有顶级变量的名称Tcl 单选按钮作为一个,tcl,tk,Tcl,Tk,我正在从数据库构建一个对话框,并使用键作为单选按钮的变量。每当我单击单选按钮时,同一列中的所有按钮都会更改。字典中有顶级变量的名称 #!/usr/bin/tclsh package require Tk variable dict_config set dict_config [dict create] dict set dict_config config_single_step { 0 "Single step" "Single step" "Run" } dict set dict_co
#!/usr/bin/tclsh
package require Tk
variable dict_config
set dict_config [dict create]
dict set dict_config config_single_step { 0 "Single step" "Single step" "Run" }
dict set dict_config config_load { 0 "Load" "No load on startup" "Load oad on startup" }
dict set dict_config config_news { 0 "News" "No news" "News Feed" }
# Callback to set the value
proc SetValue { ix val } {
puts "Setting $ix to $val"
variable dict_config
set list_item [dict get $dict_config $ix]
dict set dict_config $ix [lreplace $list_item 0 0 $val]
}
proc todo {} {
puts "Coming soon to a screen near you"
}
proc DisplayOptions { dict_config } {
set row 0
dict for { ix list_item } $dict_config {
# Extract data from the list
set lab [lindex $list_item 1]
set zero [lindex $list_item 2]
set one [lindex $list_item 3]
incr row
# set dummy variable so the radio buttons respond correctly.
set $ix [lindex $list_item 0]
upvar 0 $ix debvar
# Create widgets
label .lab_$row -text $lab
radiobutton .zero_$row -text $zero -variable debvar -value 0 -command "SetValue $ix 0"
radiobutton .one_$row -text $one -variable debvar -value 1 -command "SetValue $ix 1"
if { $debvar == 0 } {
.zero_$row select
} else {
.one_$row select
}
# Layout widgets
grid .lab_$row -sticky e -row $row -column 0
grid .zero_$row -sticky w -row $row -column 1
grid .one_$row -sticky w -row $row -column 2
}
incr row
grid [button .butSave -text "Save" -command "todo"] -row $row -column 0
}
# Let the user change them
DisplayOptions $dict_config
我也试过$debvar——同样的事情也发生了。如果我将循环体更改为
# Extract data from the list
set lab [lindex $list_item 1]
set zero [lindex $list_item 2]
set one [lindex $list_item 3]
incr row
# set dummy variable so the radio buttons respond correctly.
set r_$row [lindex $list_item 0]
# Create widgets
label .lab_$row -text $lab
radiobutton .zero_$row -text $zero -variable r_$row -value 0 -command "SetValue $ix 0"
radiobutton .one_$row -text $one -variable r_$row -value 1 -command "SetValue $ix 1"
if { r_$row == 0 } {
.zero_$row select
} else {
.one_$row select
}
# Layout widgets
grid .lab_$row -sticky e -row $row -column 0
grid .zero_$row -sticky w -row $row -column 1
grid .one_$row -sticky w -row $row -column 2
我只是想知道为什么r_$row会起作用,即按钮不会作为一个按钮更改,但是$debvar(upvar别名)不会更改 小部件的-variable选项指的是一个全局变量。这与DisplayOptions过程中的debvar的作用域不同。他们碰巧同名,但在其他方面根本没有关系 通过对所有单选按钮使用相同的全局变量,它们都作为一个组工作。要有多组单选按钮,必须为每个组使用不同的全局变量,就像使用r_u$row版本一样。请再次注意,proc中的r_$row局部变量与用于小部件的r_$row变量没有关系 实际上,最好使用另一个(更简单的)局部变量,因为
if{r_$row==0}
将始终为false,并且获取名为r_$row的变量的值过于复杂。问题在于“共享”变量debvar。您忘记了对每一行应用相同的方法,通过这种方式,您可以执行以下操作:
# set dummy variable so the radio buttons respond correctly.
set $ix [lindex $list_item 0]
upvar 0 $ix debvar_$row;# <-- Here add the row number
# Create widgets
label .lab_$row -text $lab
;#<-- Below these two lines are changed too
radiobutton .zero_$row -text $zero -variable debvar_$row -value 0 -command "SetValue $ix 0"
radiobutton .one_$row -text $one -variable debvar_$row -value 1 -command "SetValue $ix 1"
;#<-- Finally you must use a way of dereferencing in order to use each debvar
if { [set debvar_$row] == 0 } {
.zero_$row select
} else {
.one_$row select
}
#设置虚拟变量,使单选按钮正确响应。
设置$ix[lindex$列表\项目0]
upvar 0$ix debvar_$row;#这是正确的吗?单选按钮中的r_$行实际上是::r_$行,与单选按钮创建上方和下方的set和if语句中的r_$行无关。是的,这是正确的。如果在集合中使用一个名为state和If的变量,则代码的工作原理完全相同。我认为upvar使debvar成为$ix的别名,因此会有不同的debvar,但根据shelte,radiobutton中使用的是全局变量,与upvar不同。您可以避免upvar,但如果不为每行使用变量,问题仍然存在。