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

# Demonstrate how to create custom button using the Ttk theme package.
#
# The basic intent of the theme package is to have the visible elements
# of Tk controls being drawn by the platform theme engine. So overriding
# this is not really recommended. However, it is still possible to create
# alternate color backgrounds for buttons by defining a new layout for the
# button widget that includes additional or replacement elements.
# In this case, we inject a 'fill' element that will fill its drawable area
# with the style defined background color.
# Note that this declares a new *style*. All buttons with this style will
# have the same background color. It does not permit you to color the
# buttons on a widget by widget basis. If you want to do that, you should
# reconsider your UI design because such interfaces are always hideous.
#

package require Tk 8.5

# Initialize the custom style elements within the current theme and
# hook the theme changed event so that we redefine the elements in any
# new theme chosen at runtime by the end-user.
proc InitTheme {} {

    # Modify the current style definition for a Ttk button.
    set layout [ttk::style layout TButton]
    set tail [lindex $layout end]
    set tail [list Plain.Button.fill -sticky news -children $tail]
    ttk::style layout Plain.Button [lreplace $layout end end $tail]

    # Copy the standard configuration and override the background
    ttk::style configure Plain.Button {*}[ttk::style configure TButton] \
        -background SteelBlue
    ttk::style map Plain.Button {*}[ttk::style map TButton] \
        -background {active LightSteelBlue}
    
    if {[lsearch -exact [bind . <<ThemeChanged>>] InitTheme] == -1} {
        bind . <<ThemeChanged>> +InitTheme
    }
}

InitTheme

wm title . "Coloured theme button demo"
ttk::frame .f -height 320 -width 630
ttk::combobox .f.themes -values [ttk::style theme names]
.f.themes set [ttk::style theme use]
bind .f.themes <<ComboboxSelected>> {ttk::style theme use [%W get]}
ttk::button .f.b -text Demo -style Plain.Button
ttk::button .f.n -text Standard
grid .f.themes .f.b .f.n -sticky ew
grid rowconfigure .f 1 -weight 1
grid columnconfigure .f 0 -weight 1
pack .f -fill both -expand 1 -ipadx 5 -ipady 5
bind . <Control-F2> {console show}
tkwait window .
exit