I want to bind an action to two commands that will be done one after the other. I’ve tried the following syntax:
$g marker bind $point <Button-1> [list $table HighLightCell 1 [expr [lindex $value 1] + 3] \; __highlight_selected $table $sparam $eyes sparam_eyes $graph]
I’ve also tried few other variations, but none of them worked.
$g is a blt graph, and it handles binds like normal binds in Tk. What happens is that Tcl thinks that all the parameters are parameters for $table HighLightCell and doesn’t interprate the ; as the end of one command and start of a new command.
I can’t use {...} syntax because I need to put variables in the commands, which I’m not able to do using {...}. How can I put 2 commands that will be done when the event happens, without creating a new proc that will call those commands?
Even if I agree that the
procway is the better one, you can try the following two ways.The first one, is to use double quotes for grouping,
"...", which are similar to braces, but allows variable expansion and command substitution. Your code will be something like:(Note that the semicolon is not escaped.)
The second one, is to use
eval, and to defer the execution of the two commands to it. You can try this:evaljust calls the interpreter on a command formed by its arguments. If among them there is a semicolon, the commands will be two. The semicolon must be escaped, this time, because it must be interpreted by theevaland not before.I tried both the methods with the following simple codes, and they both works:
Clicking on either the buttons gives the same result, which is the expected
One more thing, it’s better to put the arguments of the
exprcommand inside braces, so in your code you should replacewith
You don’t have to worry about the substitution of the
$valuevariable and about the execution of thelindexcommand:exprdoes that for you, but it also optimize the execution. You can find some more information in the wiki (http://wiki.tcl.tk/583), or in theexprmanual page (http://www.tcl.tk/man/tcl8.5/TclCmd/expr.htm).