1# check.tcl -- 2# 3# This demonstration script creates a toplevel window containing 4# several checkbuttons. 5# 6# RCS: @(#) $Id$ 7 8if {![info exists widgetDemo]} { 9 error "This script should be run from the \"widget\" demo." 10} 11 12package require Tk 13 14set w .check 15catch {destroy $w} 16toplevel $w 17wm title $w "Checkbutton Demonstration" 18wm iconname $w "check" 19positionWindow $w 20 21label $w.msg -font $font -wraplength 4i -justify left -text "Four checkbuttons are displayed below. If you click on a button, it will toggle the button's selection state and set a Tcl variable to a value indicating the state of the checkbutton. The first button also follows the state of the other three. If only some of the three are checked, the first button will display the tri-state mode. Click the \"See Variables\" button to see the current values of the variables." 22pack $w.msg -side top 23 24## See Code / Dismiss buttons 25set btns [addSeeDismiss $w.buttons $w [list safety wipers brakes sober]] 26pack $btns -side bottom -fill x 27 28checkbutton $w.b0 -text "Safety Check" -variable safety -relief flat \ 29 -onvalue "all" \ 30 -offvalue "none" \ 31 -tristatevalue "partial" 32checkbutton $w.b1 -text "Wipers OK" -variable wipers -relief flat 33checkbutton $w.b2 -text "Brakes OK" -variable brakes -relief flat 34checkbutton $w.b3 -text "Driver Sober" -variable sober -relief flat 35pack $w.b0 -side top -pady 2 -anchor w 36pack $w.b1 $w.b2 $w.b3 -side top -pady 2 -anchor w -padx 15 37 38## This code makes $w.b0 function as a tri-state button; it's not 39## needed at all for just straight yes/no buttons. 40 41set in_check 0 42proc tristate_check {n1 n2 op} { 43 global safety wipers brakes sober in_check 44 if {$in_check} { 45 return 46 } 47 set in_check 1 48 if {$n1 eq "safety"} { 49 if {$safety eq "none"} { 50 set wipers 0 51 set brakes 0 52 set sober 0 53 } elseif {$safety eq "all"} { 54 set wipers 1 55 set brakes 1 56 set sober 1 57 } 58 } else { 59 if {$wipers == 1 && $brakes == 1 && $sober == 1} { 60 set safety all 61 } elseif {$wipers == 1 || $brakes == 1 || $sober == 1} { 62 set safety partial 63 } else { 64 set safety none 65 } 66 } 67 set in_check 0 68} 69 70trace variable wipers w tristate_check 71trace variable brakes w tristate_check 72trace variable sober w tristate_check 73trace variable safety w tristate_check 74