ramsan says: A typical problem when trying to define the command script is when you want to mix variables that need to be substituted now and others that need to be substituted later. Some examples:
button $b -command [list $b configure -background red] button $b -command "[list $b configure -background red] ; [list puts \ "pressed $b"]"
button $b -command { set aa $some_global_var }
button $b -command [list some_function $b] ;# recommended solution
button $b -command [string map [list %W [list $b]] { set aa $some_global_var %W configure -background red }]
or:
set cmd { set aa $some_global_var %W configure -background red } button $b -command [string map [list %W [list $b]] $cmd]
it is the same but different disposition.
There are other solutions that become easily complex and error prone:
button $b -command " set aa \$some_global_var $b configure -background red "
DGP Actually, your last three examples are all buggy. They will all fail if the value of $b contains white space. Avoid Quoting Hell. Stick with the helper proc.
ramsan The two examples with string map should be OK now.
[ Category Example ]