Saran
Saran

Reputation: 366

setting solid color in radio button tcl tk

i want "LED" kind of radio buttons placed next to each other. I used this command to set the color and disabled the state.

radiobutton .field1 -disabledforeground green -state "disabled"
radiobutton .field2 -disabledforeground red -state "disabled"
radiobutton .field3 -disabledforeground green -state "disabled"

grid .field1 -row 0 -column 1
grid .field2 -row 0 -column 2
grid .field3 -row 0 -column 3

I want something like led kind of. Filling solid green color and turning off the indicator inside the radio button.

I tried "-indicatoron" setting to false. It doesnt work still.

My Application will look like this, enter image description here

Code:

proc changeDisabledColor {w color} {
    puts "gng inside"
    $w configure -disabledforeground $color
}

set rowList [list "row1" "row2" "row3" "row4" "row5" "row6"]
set colList [list "1" "2" "3" "4" "5" "6"]

label .textNames -text "Description"
grid .textNames -row 0 -column 0
foreach temItem $colList {
    label .field$temItem -text "col $temItem"
    grid .field$temItem -row 0 -column $temItem
}

set rowIndex 1

foreach item $rowList {
    set colIndex 0
    label .$item -text "$item"
    grid .$item -row $rowIndex -column $colIndex
    foreach temCol $colList {
        set frameId "frame_$item\_$temCol"
        frame .$frameId -borderwidth 2 -relief ridge
        grid .$frameId -row $rowIndex -column [expr $colIndex + 1]
        radiobutton .$frameId.field1 -disabledforeground green -state "disabled"
        radiobutton .$frameId.field2 -disabledforeground red -state "disabled"
        radiobutton .$frameId.field3 -disabledforeground green -state "disabled"

        grid .$frameId.field1 -row $rowIndex -column [expr $colIndex + 1]
        grid .$frameId.field2 -row $rowIndex -column [expr $colIndex + 2]
        grid .$frameId.field3 -row $rowIndex -column [expr $colIndex + 3]
        incr colIndex
    }
    incr rowIndex
}

bind .frame_row3_2.field3 <Map> [list after 10000 {changeDisabledColor %W blue}]

Expected Output:

enter image description here

Is it possible to make radio button look like this?

Upvotes: 0

Views: 341

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137707

Firstly, what you are trying to do is not portable. It will not work on all platforms, some of which override the way that radiobuttons look quite thoroughly. You'd be better off using labels and configuring the images in them. Except…

Secondly, it's looking like it will create a large number of widgets. That's usually an indication that you're not doing things the right way. Such as…

Thirdly, it's really not that difficult to use a canvas, and that gives you both efficiency and flexibility.

pack [canvas .c]

# Some helper procedures
proc makeClickableDot {x y bgVariable callback} {
    upvar #0 $bgVariable background
    set coords [list $x $y [expr {$x+10}] [expr {$y+10}]]
    set id1 [.c create oval $coords -fill $background -outline grey75]
    set id2 [.c create arc $coords -style arc -start 45 -extent 180 -outline black]
    .c bind $id1 <1> $callback
    .c bind $id2 <1> $callback
    trace add variable background write [list clickableDotWrite $bgVariable $id1]
}
proc clickableDotWrite {bgVariable id args} {
    upvar #0 $bgVariable background
    .c itemconf $id -fill $background
}

# Now we can just make our variables and dots
set rowList [list "row1" "row2" "row3" "row4" "row5" "row6"]
set colList [list "1" "2" "3" "4" "5" "6"]

set Y 10
foreach row $rowList {
    set X 10
    foreach col $colList {
        set cell($row,$col,1) green
        # It's a dumbass callback!
        makeClickableDot $X $Y cell($row,$col,1) [list set cell($row,$col,1) red]
        incr X 15
        set cell($row,$col,2) blue
        makeClickableDot $X $Y cell($row,$col,2) [list set cell($row,$col,2) yellow]
        incr X 15
        set cell($row,$col,3) magenta
        makeClickableDot $X $Y cell($row,$col,3) [list set cell($row,$col,3) cyan]
        incr X 15
    }
    incr Y 15
}

OK, that's very colourful and you'll need a bit more work to tune it to how you want (I've not totally nailed the look, the callbacks are dumb and you probably need a second array to hold some sort of state) but it's the core of it all and all you need to do after setting it up is manipulate variables. The traces take care of reflecting the changes to those to the GUI. Which is nice, and how Tk is supposed to work most of the time.

Upvotes: 1

Related Questions