单选按钮表现为一体

时间:2018-09-09 07:33:19

标签: 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_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别名。

2 个答案:

答案 0 :(得分:1)

小部件的-variable选项引用全局变量。因此,其范围与DisplayOptions proc中的debvar不同。它们恰好具有相同的名称,但在其他方面完全没有关系。

通过对所有单选按钮使用相同的全局变量,它们全部作为一组工作。要具有多组单选按钮,您必须为每个组使用不同的全局变量,就像使用r_ $ row版本一样。请再次注意,proc中的r_ $ row局部变量与用于小部件的r_ $ row变量无关。

实际上,最好使用其他(更简单的)局部变量,因为if { r_$row == 0 }始终为false,并且获取名称构造为r_ $ row的变量的值过于复杂。

答案 1 :(得分:1)

问题出在“共享”变量 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
        }

希望对您有帮助

礼炮!

相关问题