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

  • Home
  • SEARCH
  • 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 6120495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:40:30+00:00 2026-05-23T15:40:30+00:00

I can’t bind the Escape and Return keys in my Tcl/Tk code. The following

  • 0

I can’t bind the Escape and Return keys in my Tcl/Tk code. The following code reproduces the bug. When I hit Esc or Enter key I get the following error message:

Error: can’t read “cmd”: no such variable

proc OkCancel { cmd } {
  button .${cmd}.ok -text "OK" -command [list myOk $cmd ]
  button .${cmd}.cancel -text "Cancel" -command [list myCancel .$cmd]
  grid .${cmd}.ok .${cmd}.cancel -sticky e

  bind .$cmd <Return> { myOk $cmd ; break }
  bind .$cmd <Escape> { myCancel .${cmd} ; break }
}

proc myOk { cmd } {
  puts "The command name is = $cmd"
}

proc myCancel { arg } {
  destroy $arg
}

proc test { } {
  set cmd "hello"
  toplevel .$cmd
  OkCancel $cmd
}

test
  • 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-23T15:40:30+00:00Added an answer on May 23, 2026 at 3:40 pm

    That’s because when an event fires, the script bound to it gets evaluated at the global level (as per the bind manual). That is, in case of your <Return> binding, the script myOk $cmd ; break will be executed, literally. Hence, if there exists no variable named “cmd” in the global namespace at the time your script runs, the error you have shown will be triggered.

    To fix the problem, there are several ways:

    • Make the $cmd substitution be evaluated at the time the script is created and bound. To do this, just replace {} with “” to allow variable substitution, that is, if you’d write bind .$cmd <Return> " myOk $cmd ; break " the bound script would be myOK hello ; break
    • Turn the callback script into a procedure call and pass it explicit parameters, like this: bind .$cmd <Return> [list mycallback $cmd] and then make sure you have defined mycallback procedure accepting one parameter. This way you can write generic event handlers which are parameterized by whatever parameters you need at the time of the binding.
    • Use namespace code or a similar tool to make your script executed in a specified namespace which has the specified variable defined.

    In all cases beware that since your $cmd may in certain cases expand to something odd, it’s a good idea to protect the whole script from such a situation–that’s what [list ...] does in the second example (see this for more info).

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

Sidebar

Related Questions

Can anyone give me an example of how to return the following json simply
Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I have a jquery bug and I've been looking for hours now, I can't
Can anybody tell what is the best(easy) way to sort the following string 'index'
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can we close all known/unknown connections to database with the code? I'm using Access
Can i get the source code for a WAMP stack installer somewhere? Any help
can any one tell me how can change this java code into objective c.is
Can we do following in SQL Server 2005 query with convert function. MaxRegID =

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.