home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / tcl / 2349 < prev    next >
Encoding:
Text File  |  1993-01-12  |  2.9 KB  |  71 lines

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!psinntp!sugar!karl
  3. From: karl@NeoSoft.com (Karl Lehenbauer)
  4. Subject: Re: Tk/Wish beginner seeks advice on use of text widget
  5. Organization: NeoSoft Communications Services -- (713) 684-5900
  6. Date: Wed, 13 Jan 1993 00:29:04 GMT
  7. Message-ID: <C0ro0H.6JA@NeoSoft.com>
  8. References: <1itak6INNqr2@darkstar.UCSC.EDU>
  9. Lines: 60
  10.  
  11. In article <1itak6INNqr2@darkstar.UCSC.EDU> de@helios.UCSC.EDU (De Clarke) writes:
  12. >I can't figure out why the tcl executed with the -command flag from
  13. >a wish script will recognize some variables but not others.
  14. >For example, when the "load" button is pressed (see below), although
  15. >text has been entered in the text widget $w.bot.raval, I get a tcl
  16. >error no such variable.
  17.  
  18. This is a very common source of confusion for new Tk programmers.
  19.  
  20. Your difficulties stem from the need to understand how quoting affects
  21. variable substitution and square bracket evaulation, and when you want 
  22. variables substituted and when you don't want them substituted.
  23.  
  24. >    button $w.bot.load -text "load" -command \
  25. >        "set entry [${w.bot.raval} get 1.0 end]"
  26.  
  27. See, here you want $w to be expanded when the button is created,
  28. but you don't want the square-bracketed part to execute until
  29. the button is pressed, so you need:
  30.  
  31.     button $w.bot.load -text "load" -command \
  32.         "set entry \[$w.bot.raval get 1.0 end\]"
  33.  
  34. Since you're using double quotes, the insides of the command will be
  35. evaluated.  (They wouldn't be if you'd used curly brackets.)
  36.  
  37. If you don't force "w" to be expanded at button creation time, a
  38. variable "w" must exist as a global at button execution time.
  39.  
  40. >Before I put the curlies around $w.bot.raval, tcl complained
  41. >about "w" being undefined -- couldn't handle the dots in the var name.
  42. >Tcl appears to be allergic to variable names containing "." whereas
  43. >wish uses them routinely.  [?]
  44.  
  45. Nah, dot terminates a varname, so $w.foo will get the value of w
  46. and concatenate it with .foo.
  47.  
  48. >button $w.bot.dismiss -text "go away" -command \
  49. >    "puts stdout $var1; puts stdout $var2; destroy $w"
  50.  
  51. >In this instance, var1 and var2 were correctly written to stdout,
  52. >but window $w was not destroyed ("No such variable $w") 
  53.  
  54. Are you *sure* you didn't use curlies rather than double quotes?  i.e.
  55.  
  56. button $w.bot.dismiss -text "go away" -command \
  57.     {puts stdout $var1; puts stdout $var2; destroy $w}
  58.  
  59. ...because your example works fine for me.  With the curlies instead of
  60. quotes, see, the $w isn't expanded until button press time, at which time
  61. there is no w variable (unless it's a global).  This is where it gets
  62. tricky, like w is known at button creation time, but you're going to want
  63. var1 and var2 to be picked up from globals when the button is pressed, you
  64. have to do something like:
  65.  
  66. button $w.bot.dismiss -text "go away" -command \
  67.     "puts stdout \$var1; puts stdout \$var2; destroy $w"
  68. -- 
  69. -- Email info@NeoSoft.com for info on getting interactive Internet access.
  70. "In a minute, I'll burp up your droid."
  71.