ttkbut.tcl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. set w .ttkbut
  11. catch {destroy $w}
  12. toplevel $w
  13. wm title $w "Simple Ttk Widgets"
  14. wm iconname $w "ttkbut"
  15. positionWindow $w
  16. 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."
  17. pack $w.msg -side top -fill x
  18. ## See Code / Dismiss
  19. pack [addSeeDismiss $w.seeDismiss $w {enabled cheese tomato basil oregano happiness}]\
  20. -side bottom -fill x
  21. ## Add buttons for setting the theme
  22. ttk::labelframe $w.buttons -text "Buttons"
  23. foreach theme [ttk::themes] {
  24. ttk::button $w.buttons.$theme -text $theme \
  25. -command [list ttk::setTheme $theme]
  26. pack $w.buttons.$theme -pady 2
  27. }
  28. ## Helper procedure for the top checkbutton
  29. proc setState {rootWidget exceptThese value} {
  30. if {$rootWidget in $exceptThese} {
  31. return
  32. }
  33. ## Non-Ttk widgets (e.g. the toplevel) will fail, so make it silent
  34. catch {
  35. $rootWidget state $value
  36. }
  37. ## Recursively invoke on all children of this root that are in the same
  38. ## toplevel widget
  39. foreach w [winfo children $rootWidget] {
  40. if {[winfo toplevel $w] eq [winfo toplevel $rootWidget]} {
  41. setState $w $exceptThese $value
  42. }
  43. }
  44. }
  45. ## Set up the checkbutton group
  46. ttk::labelframe $w.checks -text "Checkbuttons"
  47. ttk::checkbutton $w.checks.e -text Enabled -variable enabled -command {
  48. setState .ttkbut .ttkbut.checks.e \
  49. [expr {$enabled ? "!disabled" : "disabled"}]
  50. }
  51. set enabled 1
  52. ## See ttk_widget(n) for other possible state flags
  53. ttk::separator $w.checks.sep1
  54. ttk::checkbutton $w.checks.c1 -text Cheese -variable cheese
  55. ttk::checkbutton $w.checks.c2 -text Tomato -variable tomato
  56. ttk::separator $w.checks.sep2
  57. ttk::checkbutton $w.checks.c3 -text Basil -variable basil
  58. ttk::checkbutton $w.checks.c4 -text Oregano -variable oregano
  59. pack $w.checks.e $w.checks.sep1 $w.checks.c1 $w.checks.c2 $w.checks.sep2 \
  60. $w.checks.c3 $w.checks.c4 -fill x -pady 2
  61. ## Set up the radiobutton group
  62. ttk::labelframe $w.radios -text "Radiobuttons"
  63. ttk::radiobutton $w.radios.r1 -text "Great" -variable happiness -value great
  64. ttk::radiobutton $w.radios.r2 -text "Good" -variable happiness -value good
  65. ttk::radiobutton $w.radios.r3 -text "OK" -variable happiness -value ok
  66. ttk::radiobutton $w.radios.r4 -text "Poor" -variable happiness -value poor
  67. ttk::radiobutton $w.radios.r5 -text "Awful" -variable happiness -value awful
  68. pack $w.radios.r1 $w.radios.r2 $w.radios.r3 $w.radios.r4 $w.radios.r5 \
  69. -fill x -padx 3 -pady 2
  70. ## Arrange things neatly
  71. pack [ttk::frame $w.f] -fill both -expand 1
  72. lower $w.f
  73. grid $w.buttons $w.checks $w.radios -in $w.f -sticky nwe -pady 2 -padx 3
  74. grid columnconfigure $w.f {0 1 2} -weight 1 -uniform yes