Kuo-Hsien Chang
Kuo-Hsien Chang

Reputation: 935

choose either radiobutton or scale

I am working on an interface in tk/tcl.

In the interface, there are several radiobuttons and scales (linear sliders) with entry to show the slider's numbers.

Could you please provide an example about how to select either one radiobutton or one scale?

Therefore, I want to put argument -variable and -value to radiobuttons and scales. And then, I can use switch {} ...Case {} ... to make action when one of radiobutton or scale is selected.

Thanks for your help and example.

Upvotes: 0

Views: 27

Answers (1)

Jerry
Jerry

Reputation: 71578

Something like that I guess?

package require Tk

set radios [list .r1 .r2 .r3]
set scales [list .s1 .s2 .s3]
lassign {0 0 0} r1 r2 r3

radiobutton .r1 -text "radio1" -command {freeze radio .r1} -variable r1
radiobutton .r2 -text "radio2" -command {freeze radio .r2} -variable r2
radiobutton .r3 -text "radio3" -command {freeze radio .r3} -variable r3

scale .s1 -orient horizontal -length 200 -from 0 -to 100 -command {freeze scale .s1} \
    -tickinterval 20
scale .s2 -orient horizontal -length 200 -from 0 -to 100 -command {freeze scale .s2} \
    -tickinterval 20
scale .s3 -orient horizontal -length 200 -from 0 -to 100 -command {freeze scale .s3} \
    -tickinterval 20

# Disables all the other widgets of the same 'type' except the one touched
proc freeze {type w args} {
    switch $type {
        radio {
            upvar radios radios
            foreach n $radios {
                if {$n != $w} {
                    $n configure -state disabled
                }
            }
        }
        scale {
            upvar scales scales
            foreach n $scales {
                if {$n != $w} {
                    $n configure -state disabled
                }
            }
        }
    }
}

pack {*}$radios {*}$scales

Though there are many ways to do what you have described in your question, if I understood it properly. There might be more appropriate ways to do it depending on your situation, but that much is hard to say without seeing the actual code. I tried to make the example as simple as possible.

Upvotes: 1

Related Questions