2

I've got this small loop cycle in TCL

for {set i 1} {$i <= $user} {incr i} {
   grid [ttk::button .seluser.$i -text "$i" -command { set ::user $i }] -column $i -row 1
}

and I'm getting the message

ERROR can't read "i": no such variable

I think it's because -command works like a new proc and that's why it can not identify the variable i.

I don't know how to do it. Can anybody help me?

1 Answer 1

2

Try quotes instead of braces, so that $i is pre-interpolated. For example,

for {set i 1} {$i <= $user} {incr i} {
    grid [ttk::button .seluser.$i -text "$i" -command " set ::user $i "] -column $i -row 1
} 
Sign up to request clarification or add additional context in comments.

2 Comments

It is recommended practice to use list to construct define-time substituted commands, like this: [list set ::user $i]. This is recommended because it is both systematically guaranteed to be correct (Tcl likes to build lists that are also substitution-free commands) and faster to execute (basically, the value can carry a proof with it that no parsing is required).
It would be good to discuss why quotes work instead of braces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.