[glennj] Some time ago [http://groups.google.ca/group/comp.lang.tcl/browse_frm/thread/29d55b62b754b484], I because frustrated with Tk's default checkbutton. I wanted it to accept any boolean value [http://www.tcl.tk/man/tcl8.4/TclLib/GetInt.htm]. I ended up writing this package: ---- package require Tk package provide booleanCheckbutton 0.1 # create a checkbutton that selects when it's variable is set to # a boolean true value # # arguments: # widget - the widget "pathName" # args - list of checkbutton configuration options # # example: # set cb [booleanCheckbutton .bcb -textvariable bcbVal \ # -text "checkbutton label" -relief flat] # set bcbVal true # # Any errors will be propagated up to the caller. # namespace eval ::booleanCheckbutton { namespace export booleanCheckbutton } proc ::booleanCheckbutton::booleanCheckbutton {pathName args} { # create the checkbutton with the specified options eval [linsert $args 0 checkbutton $pathName] set varName [$pathName cget -variable] set trace_cmd [list [namespace current]::selectIfTrue $pathName] # ensure the trace occurs in the global scope uplevel #0 [list trace remove variable $varName write $trace_cmd] uplevel #0 [list trace add variable $varName write $trace_cmd] # trigger the trace for the current value of $varName, if it exists upvar #0 $varName var if {[info exists var]} { uplevel #0 [list set $varName $var] } return $pathName } proc ::booleanCheckbutton::selectIfTrue {widget varName arrayIndex op} { if {$op eq "write"} { if {$arrayIndex eq ""} { upvar #0 $varName var } else { upvar #0 ${varName}($arrayIndex) var } if {[string is boolean -strict $var] && $var} { set var [$widget cget -onvalue] } } } if 0 { # test package require Tk package require booleanCheckbutton namespace import ::booleanCheckbutton::booleanCheckbutton entry .e -textvar entry_value bind .e {set checkbutton_value $entry_value; set entry_value ""} booleanCheckbutton .c -variable checkbutton_value -textvariable checkbutton_value button .l -text "type a value, hit return, observe the checkbutton" pack .l .e .c } ---- [glennj] 20071202 - updated to fix an error in the test code, and placed package in a namespace ---- [Category Widget]