ttk::style short simple example

Here is a short script which gives a simple example of ttk::style usage.

It displays buttons and randomly set a custom style to some of them.

#
# Experiment on ttk::style
#

package require Tk;

set MAX 24
set COL 4
ttk::setTheme "clam"

ttk::style configure style1.TButton -background goldenrod
ttk::style configure style2.TButton -background gold
ttk::style configure style3.TButton -background {lemon chiffon}

proc whatClassStyle {widget} {
         set wClass [winfo class ${widget}] 
         set wStyle [${widget} cget -style] 
         .label configure -text "button ${widget} class is ${wClass}. Style is ${wStyle}"         
}

set k 0 

while {$k < $MAX} {               
        set textbutton [format "%02d" $k]
        set widget ".btn$k"
        set btn$k [ttk::button ${widget} -text $textbutton]
        set alea [expr int(rand()*4)]

        if {${alea} > 0} {
              set aleaStyle "style${alea}.TButton"
              ${widget} configure -style ${aleaStyle}
        }
       ${widget} configure -command [list whatClassStyle ${widget}]
       incr k
}

set k 0

while {$k < $MAX} {
       set j 0 
       set cmd [list grid]

       while {$j < $COL} {
              lappend cmd .btn$k
              incr j 
              incr k
       }
       eval $cmd
}

set label [ttk::label .label -text "..." ]
grid  .label -columnspan $COL