check.tcl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # check.tcl --
  2. #
  3. # This demonstration script creates a toplevel window containing
  4. # several checkbuttons.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .check
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Checkbutton Demonstration"
  13. wm iconname $w "check"
  14. positionWindow $w
  15. label $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."
  16. pack $w.msg -side top
  17. ## See Code / Dismiss buttons
  18. set btns [addSeeDismiss $w.buttons $w [list safety wipers brakes sober]]
  19. pack $btns -side bottom -fill x
  20. checkbutton $w.b0 -text "Safety Check" -variable safety -relief flat \
  21. -onvalue "all" \
  22. -offvalue "none" \
  23. -tristatevalue "partial"
  24. checkbutton $w.b1 -text "Wipers OK" -variable wipers -relief flat
  25. checkbutton $w.b2 -text "Brakes OK" -variable brakes -relief flat
  26. checkbutton $w.b3 -text "Driver Sober" -variable sober -relief flat
  27. pack $w.b0 -side top -pady 2 -anchor w
  28. pack $w.b1 $w.b2 $w.b3 -side top -pady 2 -anchor w -padx 15
  29. ## This code makes $w.b0 function as a tri-state button; it's not
  30. ## needed at all for just straight yes/no buttons.
  31. set in_check 0
  32. proc tristate_check {n1 n2 op} {
  33. global safety wipers brakes sober in_check
  34. if {$in_check} {
  35. return
  36. }
  37. set in_check 1
  38. if {$n1 eq "safety"} {
  39. if {$safety eq "none"} {
  40. set wipers 0
  41. set brakes 0
  42. set sober 0
  43. } elseif {$safety eq "all"} {
  44. set wipers 1
  45. set brakes 1
  46. set sober 1
  47. }
  48. } else {
  49. if {$wipers == 1 && $brakes == 1 && $sober == 1} {
  50. set safety all
  51. } elseif {$wipers == 1 || $brakes == 1 || $sober == 1} {
  52. set safety partial
  53. } else {
  54. set safety none
  55. }
  56. }
  57. set in_check 0
  58. }
  59. trace variable wipers w tristate_check
  60. trace variable brakes w tristate_check
  61. trace variable sober w tristate_check
  62. trace variable safety w tristate_check