单选按钮表现为一个

问题描述 投票:1回答:2

我正在从数据库构建一个对话框,并使用该键作为单选按钮的变量。每当我点击一个单选按钮时,同一列中的所有按钮都会改变。字典中有名称的顶级变量

#!/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别名不会。

tcl tk
2个回答
1
投票

窗口小部件的变量选项是指全局变量。因此,它与DisplayOptions proc中的debvar的范围不同。它们碰巧具有相同的名称,但在其他方面根本没有任何关系。

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

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


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
        }

我希望这能帮到你,

您好!

© www.soinside.com 2019 - 2024. All rights reserved.