ttkbut.tcl 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # ttkbut.tcl --
  2. #
  3. # This demonstration script creates a toplevel window containing several
  4. # simple Ttk widgets, such as labels, labelframes, buttons, checkbuttons and
  5. # radiobuttons.
  6. if {![info exists widgetDemo]} {
  7. error "This script should be run from the \"widget\" demo."
  8. }
  9. package require Tk
  10. package require Ttk
  11. set w .ttkbut
  12. catch {destroy $w}
  13. toplevel $w
  14. wm title $w "Simple Ttk Widgets"
  15. wm iconname $w "ttkbut"
  16. positionWindow $w
  17. ttk::label $w.msg -font $font -wraplength 4i -justify left -text "Ttk is the new Tk themed widget set. This is a Ttk themed label, and below are three groups of Ttk widgets in Ttk labelframes. The first group are all buttons that set the current application theme when pressed. The second group contains three sets of checkbuttons, with a separator widget between the sets. Note that the \u201cEnabled\u201d button controls whether all the other themed widgets in this toplevel are in the disabled state. The third group has a collection of linked radiobuttons."
  18. pack $w.msg -side top -fill x
  19. ## See Code / Dismiss
  20. pack [addSeeDismiss $w.seeDismiss $w {enabled cheese tomato basil oregano happyness}]\
  21. -side bottom -fill x
  22. ## Add buttons for setting the theme
  23. ttk::labelframe $w.buttons -text "Buttons"
  24. foreach theme [ttk::themes] {
  25. ttk::button $w.buttons.$theme -text $theme \
  26. -command [list ttk::setTheme $theme]
  27. pack $w.buttons.$theme -pady 2
  28. }
  29. ## Helper procedure for the top checkbutton
  30. proc setState {rootWidget exceptThese value} {
  31. if {$rootWidget in $exceptThese} {
  32. return
  33. }
  34. ## Non-Ttk widgets (e.g. the toplevel) will fail, so make it silent
  35. catch {
  36. $rootWidget state $value
  37. }
  38. ## Recursively invoke on all children of this root that are in the same
  39. ## toplevel widget
  40. foreach w [winfo children $rootWidget] {
  41. if {[winfo toplevel $w] eq [winfo toplevel $rootWidget]} {
  42. setState $w $exceptThese $value
  43. }
  44. }
  45. }
  46. ## Set up the checkbutton group
  47. ttk::labelframe $w.checks -text "Checkbuttons"
  48. ttk::checkbutton $w.checks.e -text Enabled -variable enabled -command {
  49. setState .ttkbut .ttkbut.checks.e \
  50. [expr {$enabled ? "!disabled" : "disabled"}]
  51. }
  52. set enabled 1
  53. ## See ttk_widget(n) for other possible state flags
  54. ttk::separator $w.checks.sep1
  55. ttk::checkbutton $w.checks.c1 -text Cheese -variable cheese
  56. ttk::checkbutton $w.checks.c2 -text Tomato -variable tomato
  57. ttk::separator $w.checks.sep2
  58. ttk::checkbutton $w.checks.c3 -text Basil -variable basil
  59. ttk::checkbutton $w.checks.c4 -text Oregano -variable oregano
  60. pack $w.checks.e $w.checks.sep1 $w.checks.c1 $w.checks.c2 $w.checks.sep2 \
  61. $w.checks.c3 $w.checks.c4 -fill x -pady 2
  62. ## Set up the radiobutton group
  63. ttk::labelframe $w.radios -text "Radiobuttons"
  64. ttk::radiobutton $w.radios.r1 -text "Great" -variable happyness -value great
  65. ttk::radiobutton $w.radios.r2 -text "Good" -variable happyness -value good
  66. ttk::radiobutton $w.radios.r3 -text "OK" -variable happyness -value ok
  67. ttk::radiobutton $w.radios.r4 -text "Poor" -variable happyness -value poor
  68. ttk::radiobutton $w.radios.r5 -text "Awful" -variable happyness -value awful
  69. pack $w.radios.r1 $w.radios.r2 $w.radios.r3 $w.radios.r4 $w.radios.r5 \
  70. -fill x -padx 3 -pady 2
  71. ## Arrange things neatly
  72. pack [ttk::frame $w.f] -fill both -expand 1
  73. lower $w.f
  74. grid $w.buttons $w.checks $w.radios -in $w.f -sticky nwe -pady 2 -padx 3
  75. grid columnconfigure $w.f {0 1 2} -weight 1 -uniform yes