Tcl/tk Как узнать, когда и какая радиокнопка выбрана?

Я боролся с тем, что кажется таким тривиальным =(

Мне нужно знать, какой переключатель выбран, чтобы я мог включать и отключать свою запись.

radiobutton .t.r1 -text "Default" -variable reptype -value 0
radiobutton .t.r2 -text "Custom" -variable reptype -value 1
.t.r1 select    

place .t.r1 -x 20 -y 30
place .t.r2 -x 20 -y 50

entry .t.ecustom -width 70 
place .t.ecustom -x 100 -y 50

if { $reptype == 0 } {
    .t.ecustom configure -state normal
} elseif { $reptype == 1 } {
    .t.ecustom configure -state disabled
}

Это то, что я пытаюсь, изменяя некоторые биты здесь и там, но результат никогда не бывает таким, как я хочу, в этом примере переменная reptype не распознается.


person Lucas Oliveira    schedule 10.10.2015    source источник
comment
reptype не установлено значение. Код необходимо настроить так же, как ответ JK, чтобы изменения переключателя приводили к событию.   -  person Brad Lanam    schedule 11.10.2015


Ответы (1)


Вы можете либо использовать трассировку переменной, либо использовать обратный вызов -command. В вашем случае я предлагаю использовать обратный вызов -command.

set ::reptype 0
radiobutton .t.r1 -text "Default" -variable reptype -value 0 -command entrystate
radiobutton .t.r2 -text "Custom" -variable reptype -value 1 -command entrystate
entry .t.ecustom -state disabled
grid .t.r1
grid .t.r2 .t.ecustom
grid columnconfigure .t 1 -weight 1

proc entrystate {} {
    if {$::reptype} {
        .t.ecustom configure -state normal
    } else {
        .t.ecustom configure -state disabled
    }
}
person Johannes Kuhn    schedule 11.10.2015
comment
Я мог бы использовать .t.ecustom configure -state [lindex {disabled normal} $::reptype] в качестве тела процедуры, но это та же идея. - person Donal Fellows; 11.10.2015