Posted to tcl by patthoyts at Tue Oct 04 10:53:15 GMT 2011view raw

  1. # Demonstrate how to create custom button using the Ttk theme package.
  2. #
  3. # The basic intent of the theme package is to have the visible elements
  4. # of Tk controls being drawn by the platform theme engine. So overriding
  5. # this is not really recommended. However, it is still possible to create
  6. # alternate color backgrounds for buttons by defining a new layout for the
  7. # button widget that includes additional or replacement elements.
  8. # In this case, we inject a 'fill' element that will fill its drawable area
  9. # with the style defined background color.
  10. # Note that this declares a new *style*. All buttons with this style will
  11. # have the same background color. It does not permit you to color the
  12. # buttons on a widget by widget basis. If you want to do that, you should
  13. # reconsider your UI design because such interfaces are always hideous.
  14. #
  15.  
  16. package require Tk 8.5
  17.  
  18. # Initialize the custom style elements within the current theme and
  19. # hook the theme changed event so that we redefine the elements in any
  20. # new theme chosen at runtime by the end-user.
  21. proc InitTheme {} {
  22.  
  23. # Modify the current style definition for a Ttk button.
  24. set layout [ttk::style layout TButton]
  25. set tail [lindex $layout end]
  26. set tail [list Plain.Button.fill -sticky news -children $tail]
  27. ttk::style layout Plain.Button [lreplace $layout end end $tail]
  28.  
  29. # Copy the standard configuration and override the background
  30. ttk::style configure Plain.Button {*}[ttk::style configure TButton] \
  31. -background SteelBlue
  32. ttk::style map Plain.Button {*}[ttk::style map TButton] \
  33. -background {active LightSteelBlue}
  34.  
  35. if {[lsearch -exact [bind . <<ThemeChanged>>] InitTheme] == -1} {
  36. bind . <<ThemeChanged>> +InitTheme
  37. }
  38. }
  39.  
  40. InitTheme
  41.  
  42. wm title . "Coloured theme button demo"
  43. ttk::frame .f -height 320 -width 630
  44. ttk::combobox .f.themes -values [ttk::style theme names]
  45. .f.themes set [ttk::style theme use]
  46. bind .f.themes <<ComboboxSelected>> {ttk::style theme use [%W get]}
  47. ttk::button .f.b -text Demo -style Plain.Button
  48. ttk::button .f.n -text Standard
  49. grid .f.themes .f.b .f.n -sticky ew
  50. grid rowconfigure .f 1 -weight 1
  51. grid columnconfigure .f 0 -weight 1
  52. pack .f -fill both -expand 1 -ipadx 5 -ipady 5
  53. bind . <Control-F2> {console show}
  54. tkwait window .
  55. exit
  56.