Version 1 of toggle button

Updated 2007-03-15 22:15:19 by kpv

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.?]


KPV: tile has something similar--the Toolbutton style for a checkbutton. This is designed for a button bar, so it usually looks better if you have an image in the widget.

 package require tile
 ::ttk::checkbutton .c1 -text "ON" -style Toolbutton
 pack .c1