Mark Summerfield <
[email protected]> wrote:
On Tue, 29 Jul 2025 13:21:33 -0400, saito wrote:
On 7/29/2025 1:12 PM, Mark Summerfield wrote:
I have a program that works fine but I want to move one function call
from one place to another: when I do so the program fails.
In particular I want to move the line marked *1* to *2*.
But when I do so, as shown below, I get this error:
proc form::show_modal form {
wm deiconify $form
raise $form
focus $form
grab set $form
after idle {focus $form} ;# *2* to here
}
You are preventing/delaying the normal substitution of $form here, and
since there is no variable named "form" in scope when it fires, you get
the error.
By the way, you did not just move the line, you changed ".form" to
"$form". If you change it back, it will work.
Another way to make it work:
after idle [list focus $form]
Thank you that worked great. It fixed the tiny example above,
and it also worked in the application I'm developing.
Thanks too for the explanation.
Do keep in mind several aspects of Tcl:
1) $ means variable substitution in Tcl;
2) Tcl does not have lexical scoping for variables;
3) after scripts (and Tk callback -command options) are executed with
only global variables in scope for the execution.
#3 means that a script passed to after is not executing as part of a
proc which may have set it up, it is seperately executing in the global namespace and can only see global variables by default.
#2 means that none of the local variables in the proc that setup the
after script are visible to the after script itself. In fact, given
that the defining proc returns just after setting up the after, all the
local variables are garbage collected and no longer exist when the
after script is running.
#2 also means Tcl does not, by itself, support closures. The fact that
the after script referenced local variable $form does not make the
local 'form' variable from the proc hang around to later be visible to
the after script.
All of the above is why most documentation/tutorials/books on Tk and
widget callbacks almost always recommend defining a proc to be a
callback target from a Tk -command option. It is easy to "pass
variables" into a proc in a callback or after command such that they
are available to the resulting script or callback. It is much more
tricky to dereference variables inline properly to make the same work
for a raw script snippet.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)