Anand
Anand

Reputation: 1122

Tcl/Tk: Abstracting the creation of menu buttons and commands: Can't call the commands

I am trying to abstract my menu buttons and functions associated with it to a single proc call (addMenus function below). The following code builds the menu buttons correctly but on pressing the buttons (say Open) it errors out as:

Error: invalid command name "myputs Open"

proc myputs { label } {
  puts $label
}

proc addMenus { mbar myargs } {
  foreach { arg } $myargs {
    foreach { button options } $arg {
      set x ${mbar}.[string tolower ${button}] 
      set y ${x}.menu

      menubutton $x -text $button -menu $y
      pack $x -side left
      set mdropoff [menu $y -tearoff 0]

      foreach { label command } $options {
        $mdropoff add command -label $label -command $command
      }
    }
  }
}

#----------------------------------------
# main script
#----------------------------------------
wm title . "My Gui"

# build the frame which contains menu options
set mbar .mbar
frame $mbar -relief raised -bd 2
pack  $mbar -side top -fill x 

# text box as a filler
text .myout -width 40 -height 20
pack .myout -side top -fill both -expand true

# file menu
set myargs {
  { 
    File {
    "Open ..."     { [list myputs "Open"] }
    "New  ..."     { [list myputs "New"] }
    "Save ..."     { [list myputs "Save"] }
    "Save As ..."  { [list myputs "Save As"] }
    } 
  }
  { 
    Edit {
    "Cut"          { [list myputs "Cut"] }
    "Copy"         { [list myputs "Copy"] }
    "Paste"        { [list myputs "Paste"] }
    } 
  }
}

addMenus $mbar $myargs

Upvotes: 0

Views: 1079

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137567

The issue is exactly that Tcl doesn't do any expansion of scripts nested inside data structures like that (it can't; it doesn't know what they are until you tell it). There are a few possibilities for dealing with it:

  1. Write your code to not need it (using plain myputs "Open" instead of [list myputs "Open"] in your data).
  2. Construct your data with list throughout:

    set myargs [list [list "File" \
        [list "Open ..." [list myputs "Open"]] \
        [list "New ..." [list myputs "New"]] \
        [list "Save ..." [list myputs "Save"]] \
        [list "Save As ..." [list myputs "Save As"]] \
    ] [list "Edit" \
        ...
    

    OK, it's going to give you backslashitis (or very long lines).

  3. Use a bit of trickery with uplevel and subst. From inside addMenus

    foreach { label command } $options {
      set command [uplevel 1 [list subst $command]]
      $mdropoff add command -label $label -command $command
    }
    

    That'll make your code otherwise look like what you expect (and expand any embedded variables in the calling context, which is usually what you want; if you never use variables in the menu description – or complex namespace handling – you can use a simpler set command [subst $command]). But it is significantly trickier than anything you had before as you're moving from simple calls to something more template-based.

And if you're wanting some substitutions to happen at one time and others at another, it's time to use helper procedures: your brain (and anyone maintaining the code) will thank you.

Upvotes: 2

evil otto
evil otto

Reputation: 10582

The command is a script which is evaluated at callback time. Your code is setting the command callback of the "open" menu item to [list myputs "Open"] which, if you enter in the shell will give you the same error message.

Using [list] in widget callbacks like this is frequently good practice, since it eaves you from needing to do all sorts of backslash escaping in a plain string. But here, it's not necessary. myargs can be simply

....
File {
"Open ..."     { myputs "Open" }
"New  ..."     { myputs "New" }
....

If you want the command to include some variable to be expanded or command to be run, then at some point you need to cause that expansion to happen, at the right time and in the correct scope. For example, if your menu definition was something like

File {
"Open ..."     { myputs [getString Open] }
"New  ..."     { myputs [getString New] }
}

where getString is some command to return a string, then you could put as your menu add line

    $mdropoff add command -label $label -command [uplevel #0 subst $command]

The specifics of how you would do this depends on what kind of variables you want to pass (are they local, namespaced, or global) and when they should be expanded (do you want them expanded at the time your menu is defined, or when it is invoked?)

Upvotes: 3

Related Questions