0

I try to add two buttons to the console window for convenience and to play around with it. The problem is: The first button just triggers an "exit". This works fine. The second button triggers "LogPuts", which should write all "puts" to a file additionally. It is a toggle button. And this one gives me an "invalid command name LogPuts"

The code itself works, the procedure LogPuts is written before the button creation. Here an example:

    proc LogPuts { } { ;#.... do s.th. }

    console eval {
      place [button .console.bExit -text "Exit" -command "exit"] -relx 0.93 -rely 0.0 - 
        width 62 -height 25 
      place [button .console.bLog -text "LogPuts" -command "LogPuts"] ] -relx 0.93 -rely 
        0.06 - width 62 -height 25
    }

I even tried to play with uplevel 0 when invoking the button-press to get the procedure noticed on the "toplevel", but since unexperienced with it, the outcome was expected; it did not help :)

Has anyone an idea how to solve this problem? How can I invoke the Button Code in the Console Level?

1 Answer 1

1

According to the documentation at https://www.tcl-lang.org/man/tcl8.6/TkCmd/console.htm#M5 console eval uses a separate interpreter from the one used to run normal commands. The problem here is that you are defining LogPuts in the normal interpreter but trying use it from the console interpreter. The following should work:

console eval {
  proc LogPuts { } { ;#.... do s.th. }

  place [button .console.bExit -text "Exit" -command "exit"] -relx 0.93 -rely 0.0 - 
    width 62 -height 25 
  place [button .console.bLog -text "LogPuts" -command "LogPuts"] ] -relx 0.93 -rely 
    0.06 - width 62 -height 25
}
2
  • yes, that did it. Thank you! :) However, I encountered just another problem rightafter, which came due to the nature of the completely separated 'worlds' of console and your regular TCL. I tried to 'trace' the puts command to write it to a file beforehand. This worked fine in normal TCL, but once everything was in the eval-code, .. well, you get different results :). This was an interesting experiment anyways! But to solve my problem, I seem have to use "chan" to get a hand on stdin and stdout. Well, I have not mastered this yet :). Thanks again for your response. Commented Mar 20, 2023 at 17:20
  • 1
    Oh, just for the record :) I solved the problem in a twisted way. It could be solved with the use of "consoleintepr". So, now it is essential, that the Logputs stays outside. I just changed the direct call in the button to "LogPuts" in its command to "consoleinterp eval { trace add execution puts enter _logputs }" while "_logputs" is just the outside code that handles writing of the data from puts. Works like a charm :) Commented Mar 20, 2023 at 18:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.