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

  1. Newsgroups: comp.lang.tcl
  2. Path: sparky!uunet!caen!spencer
  3. From: spencer@med.umich.edu (Spencer W. Thomas)
  4. Subject: Re: Tk/Wish beginner seeks advice
  5. Message-ID: <SPENCER.93Jan12101913@guraldi.med.umich.edu>
  6. Date: Tue, 12 Jan 93 10:19:13 EST
  7. Organization: University of Michigan
  8. In-Reply-To: de@helios.UCSC.EDU's message of 12 Jan 1993 02:32:38 GMT
  9. References: <1itak6INNqr2@darkstar.UCSC.EDU>
  10. Nntp-Posting-Host: guraldi.itn.med.umich.edu
  11. Lines: 92
  12.  
  13.  
  14.  
  15. The -command is executed at "top level", so will only recognize global
  16. variables.  However, in your example, the variable ${w.bot.raval}
  17. should be expanded when the button command is executed (since it's in
  18. ""s).  Actually, your problem is that there is no variable
  19. ${w.bot.raval}.  You really want to say $w.bot.raval (in the first
  20. case, it's looking for a variable named w.bot.raval.  In the second,
  21. it expands $w, then appends the string .bot.raval.)
  22.  
  23. > button $w.bot.dismiss -text "go away" -command \
  24. >    "puts stdout $var1; puts stdout $var2; destroy $w"
  25.  
  26. I'm confused about this one.  Are you sure this is how it appears in
  27. your program?  You didn't put it in {}s?  Because, the way you've
  28. written it, $w should get evaluated when the button is created, and so
  29. should $var1 and $var2.  Maybe a concrete example will help:
  30.  
  31. global var1 var2
  32. set var1 1
  33. set var2 2
  34.  
  35. proc makebutton {} {
  36.     # Name of window inside which we will create the button
  37.     set w .top
  38.     button $w.dismiss -text "go away" -command \
  39.         "puts stdout $var1; puts stdout $var2; destroy $w"
  40.     pack append $w $w.dismiss {top}
  41. }
  42.  
  43. # This will cause an error: "Error: can't read "var1": no such variable"
  44. # Because var1 and var2 are not declared global inside the proc makebutton
  45. makebutton
  46.  
  47. # Retype proc makebutton with a global var1 var2 declaration, and try again
  48. makebutton
  49.  
  50. # Change values of var1 and var2
  51. set var1 5
  52. set var2 6
  53.  
  54. # Now, if you press the go away button, it will print 1\n2 then
  55. # destroy the window.  This is because the variables $var1 and $var2
  56. # were evaluated when the button was created.  Try
  57. .top.dismiss configure -command
  58. # The result is
  59. # -command command Command {} {puts stdout 1; puts stdout 2; destroy .top}
  60. # Next try:
  61.  
  62. proc makebutton {} {
  63.     # Don't need global because we won't evaluate var1, var2 yet
  64.     # Name of window inside which we will create the button
  65.     set w .top
  66.     # Use {} to delay variable evaluation until later.
  67.     button $w.dismiss -text "go away" -command \
  68.         {puts stdout $var1; puts stdout $var2; destroy $w}
  69.     pack append $w $w.dismiss {top}
  70. }
  71.  
  72. # Now 
  73. .top.dismiss configure -command
  74. # gives
  75. # -command command Command {} {puts stdout $var1; puts stdout $var2; destroy $w}
  76. # But if you push the button, you get
  77. #  5
  78. # 6
  79. # can't read "w": no such variable
  80. # Because $w was local to the proc makebutton, and doesn't exist any
  81. # more when you push the button.
  82. # But, if you change var1 or var2, the printed result follows the change.
  83. #
  84. # Finally, to get it right:
  85. #
  86.  
  87. proc makebutton {} {
  88.     # Name of window inside which we will create the button
  89.     set w .top
  90.     # Quote $var1 and $var2 with \ to delay evaluation.
  91.     button $w.dismiss -text "go away" -command \
  92.         "puts stdout \$var1; puts stdout \$var2; destroy $w"
  93.     pack append $w $w.dismiss {top}
  94. }
  95.  
  96. # Now it's right:
  97. .top.dismiss configure -command
  98. # -command command Command {} {puts stdout $var1; puts stdout $var2; destroy .top}
  99.  
  100. =S
  101. --
  102. =Spencer W. Thomas         |  Info Tech and Networking, B1911 CFOB, 0704
  103.    "Genome Informatician"    |  Univ of Michigan, Ann Arbor, MI 48109
  104. Spencer.W.Thomas@med.umich.edu    |  313-747-2778, FAX 313-764-4133
  105.