Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Error: Cant read xxxxx: no such variable (in Tcl Tk)

Phase 2Phase 1If you set a Tcl (Tcl/Tk) variable within a proc, or within the callback command of a widget, that variable does not exist at run time until you have run the proc or callback procedure, or until you have initialised the variable in some other way.

Let's see a Tk example. I have created a window with two buttons. The one labelled "Press me first" creates a variable called pressed than contains the value hello. And the one labelled "Press me second" updates the label called .report with the value that's stored in the variable.


If you run the program , pressing the first button first, and the second button second, it works as illustrated at the top of this entry to the left and right.

But if you press the button labelled press me second without pressing the button labelled press me first, you get this:

No such variable


Here is the complete code that I used to generate these examples:

button .demo1 -text "Press me first" -command {
  global pressed
  set pressed hello
  }
button .demo2 -text "Press me second" -command {
  global pressed
  .report configure -text $pressed
  }
button .quit -text exit -command exit
label .report -text "---"
pack .demo1 .demo2 .report .quit


What should you do if you are getting a message such as "Error: Can't read 'pressed': no such variable"?
• check your logic
• if necessary, initialise the variable outside your callbacks
• check that you have used global if necessary (in order to share a variable from within a proc with the same variable of the same name in the main code)
• send me an email to say you found this article useful!

We run regular public Tcl and expect courses and Tcl/Tk and wish courses. For a full schedule of all of our public courses, see here




This short article was written in response to an emailed question. I answered:

It looks like you are trying to use the contents of a variable before you set it - remember that variables in Tcl are only created when you actually run the code that sets them, so a definition within a proc will only cause the variable to be created when the proc is actually run.

Your question is a very good one, and you have provided inspiration for a full example on my blog - see:

http://www.wellho.net/mouth/2040_Error-Cant-read-xxxxx-no-such-variable-in-Tcl-Tk-.html


Graham
(written 2009-02-14, updated 2009-02-15)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T216 - Tcl/Tk - Introduction to Tk
  [4460] Using Object Oriented Tcl and the Tk toolkit together - real life example - (2015-03-12)
  [4208] Tcl + Tk (Wish) - an introduction and revision example - (2013-11-15)
  [595] Add a friendly front end with Tk - (2006-02-08)

T241 - Tcl/Tk - Tcl on the Web
  [4461] Reading from a URL, and reading Json, from your Tcl script - (2015-03-12)
  [2429] Tcl scripts / processes on a web server via CGI - (2009-09-27)
  [2238] Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14)
  [1785] What is running on your network? (tcl and expect) - (2008-09-04)


Back to
The Invoker
Previous and next
or
Horse's mouth home
Forward to
httpd, Tomcat and PHP course enhancements
Some other Articles
Please Trouble me
Confidence, Customer Service and Tourism in Melksham
Wiltshire Rail Service Update
httpd, Tomcat and PHP course enhancements
Error: Cant read xxxxx: no such variable (in Tcl Tk)
The Invoker
Sticky Sessions with mod_jk (httpd to Tomcat)
The Interview and The Lift
Java Tag Libraries / how they work / Tomcat Deployment
1234567890 ... coming up on Friday 13th
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2040_Err ... l-Tk-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat