tcl

How to invoke Tcl procedure at script start


I have a tcl script, that has the toplevel "." with a button. It is possible to put this toplevel . inside a procedure, and when i start my script, the procedure will be invoked? Exemp:

Menu
proc Menu { } {
    wm title . "Menu"
    ttk::button .b -width 30 -text close -command {exit}
    grid configure .b -row 0 -column 0 -sticky nw -padx 10 -pady 10 
}

I'm getting "invalid command name "Menu" (error)".


Solution

  • Tcl literally runs commands in the order you specify. Put the call to Menu after the call to proc Menu to make it.

    And if it is a one-shot command, use an applied lambda so you don't pollute your namespace or have to use hacks with rename:

    apply {{} {
        # All the local variables you could ever want are in here
        wm title . "Menu"
        set btn [ttk::button .b -width 30 -text close -command {exit}]
        grid configure $btn -row 0 -column 0 -sticky nw -padx 10 -pady 10 
    }}