Tcl Tk 在用户按下单选按钮时隐藏对象

Tcl Tk hide Objects when user press Radiobuttons

我在 TK 的 Gui 编程中找不到任何关于事件的信息。如果用户单击单选按钮 "automatic",其他单选按钮应该不可见。

代码如下:

set base .example1;
toplevel $base
wm geometry $base 1920x1080

set frame_RadioBtnAuto [labelframe $base.frame_RadioBtnAuto \
        -text Search \
        -font {Calibri -12 bold} ];
place $frame_RadioBtnAuto -x 250 -y 135;

set rbl [radiobutton $frame_RadioBtnAuto.rbl \
        -text "Automatic"\
        -variable "[namespace current]::optionVariable" -value one ];
#Standardwahl beim start
$frame_RadioBtnAuto.rbl select
pack $rbl -side left -anchor nw;

set rb2 [radiobutton $frame_RadioBtnAuto.rb2 \
        -text "UserDef"\
        -variable "[namespace current]::optionVariable" -value two ];
pack $rb2 -side top -anchor nw;

set frame_RadioBtnUserW1 [labelframe $base.frame_RadioBtnUserACC \
        -text Window\ 1\
        -font {Calibri -12 bold} ];
place $frame_RadioBtnUserACC -x 250 -y 170;

set rba1 [radiobutton $frame_RadioBtnUserW1.rba1 \
        -text "Test1"\
        -variable "[namespace current]::optionVariable1" -value three ];
pack $rba1 -side top -anchor nw;

set rba2 [radiobutton $frame_RadioBtnUserW1.rba2 \
        -text "Test2"\
        -variable "[namespace current]::optionVariable1" -value four ];
pack $rba2 -side top -anchor nw;

让 UI 响应单选按钮的变化相当容易。您可以通过单选按钮的 -command 选项添加脚本 您可以在变量上设置写入 trace 以便更改变量会触发 UI 改变。我想你会发现后者在复杂的 UI:

之类的情况下更可靠
trace add variable [namespace current]::optionVariable write optionVariableWritten
proc optionVariableWritten args {
    variable optionVariable
    if {$optionVariable eq "one"} {
        # Conceal the UI here
    } else {
        # Reveal the UI here
    }
}

您需要仔细考虑如何隐藏和显示 UI;有时禁用复杂的 UI 而不是完全隐藏它会提供更好(即不那么令人惊讶)的体验。