Search code examples
tclvisibilityprocedureuplevel

TCL - invalid command name ... but different


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?


Solution

  • 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
    }