Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:31:41+00:00 2026-05-23T19:31:41+00:00

I am trying to abstract my menu buttons and functions associated with it to

  • 0

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”

  • I think I am not using the quotes properly. Any pointers on fixing this?
  • Also any suggestions on improving the code especially if I want to pass arguments to menubutton or menu command?
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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:31:41+00:00Added an answer on May 23, 2026 at 7:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code is very simplified. I'm trying to abstract a dispatcher context so
I'm trying to implement abstract inheritance in Django with the following code, but it
I am trying to do the following in Delphi 2010: TDataConverter = class abstract
I'm trying to write some abstract code for searching through a list of similar
I'm trying to make a button class (abstract) so I can set what function
I'm trying to achieve the following: Given an abstract class MemoryObject , that every
I'm trying to do the following: public abstract BaseClass { public virtual void ReceiveEvent(Event
I'm trying to abstract out all database code into a separate library and then
I'm trying to set up an inheritance hierarchy similar to the following: abstract class
I am trying to make most of the utility functions abstract, so I decided

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.