Version 0 of toggle button

Updated 2007-03-15 20:56:22

Although standard Tk doesn't supply a toggle button, and most developers seem content to use a checkbox in its place, Uwe Klein illustrates how easy it is to customize a button by refinement of a label:

    set ::state OFF
    label .lb -textvariable ::state -relief raised -bd 4
    bind .lb <ButtonPress-1> {
        if {"$::state" == "OFF"} {
            set ::state ON
            %W configure -relief sunken
        } else {
            set ::state OFF
            %W configure -relief raised
    label .l -textvariable ::state
    pack .lb .l -side left

[Comments: all buttons thus; deserves abstraction; backward-compatible only to 8.?]