[vtkusers] Question about tcl/tk-VTK application,..

hvidal hvidal at tesseract-tech.com
Fri Sep 11 15:09:16 EDT 2009


This has to do with tcl's 'quoting, substitution, and evaluation
rules' which is something every tcl person runs into and has to learn about.

Basically, in the below noted code, the -command
attribute, in this case, is associated with the code
puts $a; substitution is 'deferred' because the code is in
curly braces. The actual 'program' snippet to evaluate
later when -command is actually invoked is puts $a,
thereby using whatever $a is equal to /then/.

If you want to 'quote' the code so that substitution
is done immediately before the code is associated, you
can do -command "puts $a"
where the double quotes will force immediate substitution
and lock that 'filled in' code into -command argument,
i.e. whatever $a is equal to will be substituted for
$a, and /that/ code will be executed when -command
is invoked.

I may have terminology a little wrong, but I am pretty
sure this will solve your problem. As noted, this is
important in the usage of tcl (otherwise, leads to weird
little problems....).

hv

Miguel Ángel Rodríguez Florido wrote:
>     Hi all,
> 
>     Sorry, because this question is more related to tcl/tk, but I know 
> that many people that works under VTK uses Tcl/Tk..so sorry again for 
> people that not.
> 
>     My question is very simple, but I haven't found how to do that.
> 
>     I have a Tcl/Tk procedure like:
> 
>     proc myproc {vars} {
>     
>     code
>     
>       foreach a $list {
>         button .blabla.$a -text $a -command {puts $a}
>       }
> 
>     }
> 
> 
> However the foreach variable $a doesn't exist in the -command option. I 
> mean, I got an error when it runs this puts: puts $a.
>     If I do global this variable: global a, I only got the last value of 
> $a.
> 
>     So I understand that the foreach command is running first, and when 
> it finishs, the tcl run the -command option of the tk button.
> 
>     How can I access to the variable "a" in the -command button 
> option??. I mean, how can I get the value of "a" at foreach step in the 
> -command option?
> 
>     Thank you very much.
>     Best regards.
> 




More information about the vtkusers mailing list