home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.tcl
- Path: sparky!uunet!psinntp!sugar!karl
- From: karl@NeoSoft.com (Karl Lehenbauer)
- Subject: Re: Tk/Wish beginner seeks advice on use of text widget
- Organization: NeoSoft Communications Services -- (713) 684-5900
- Date: Wed, 13 Jan 1993 00:29:04 GMT
- Message-ID: <C0ro0H.6JA@NeoSoft.com>
- References: <1itak6INNqr2@darkstar.UCSC.EDU>
- Lines: 60
-
- In article <1itak6INNqr2@darkstar.UCSC.EDU> de@helios.UCSC.EDU (De Clarke) writes:
- >I can't figure out why the tcl executed with the -command flag from
- >a wish script will recognize some variables but not others.
- >For example, when the "load" button is pressed (see below), although
- >text has been entered in the text widget $w.bot.raval, I get a tcl
- >error no such variable.
-
- This is a very common source of confusion for new Tk programmers.
-
- Your difficulties stem from the need to understand how quoting affects
- variable substitution and square bracket evaulation, and when you want
- variables substituted and when you don't want them substituted.
-
- > button $w.bot.load -text "load" -command \
- > "set entry [${w.bot.raval} get 1.0 end]"
-
- See, here you want $w to be expanded when the button is created,
- but you don't want the square-bracketed part to execute until
- the button is pressed, so you need:
-
- button $w.bot.load -text "load" -command \
- "set entry \[$w.bot.raval get 1.0 end\]"
-
- Since you're using double quotes, the insides of the command will be
- evaluated. (They wouldn't be if you'd used curly brackets.)
-
- If you don't force "w" to be expanded at button creation time, a
- variable "w" must exist as a global at button execution time.
-
- >Before I put the curlies around $w.bot.raval, tcl complained
- >about "w" being undefined -- couldn't handle the dots in the var name.
- >Tcl appears to be allergic to variable names containing "." whereas
- >wish uses them routinely. [?]
-
- Nah, dot terminates a varname, so $w.foo will get the value of w
- and concatenate it with .foo.
-
- >button $w.bot.dismiss -text "go away" -command \
- > "puts stdout $var1; puts stdout $var2; destroy $w"
-
- >In this instance, var1 and var2 were correctly written to stdout,
- >but window $w was not destroyed ("No such variable $w")
-
- Are you *sure* you didn't use curlies rather than double quotes? i.e.
-
- button $w.bot.dismiss -text "go away" -command \
- {puts stdout $var1; puts stdout $var2; destroy $w}
-
- ...because your example works fine for me. With the curlies instead of
- quotes, see, the $w isn't expanded until button press time, at which time
- there is no w variable (unless it's a global). This is where it gets
- tricky, like w is known at button creation time, but you're going to want
- var1 and var2 to be picked up from globals when the button is pressed, you
- have to do something like:
-
- button $w.bot.dismiss -text "go away" -command \
- "puts stdout \$var1; puts stdout \$var2; destroy $w"
- --
- -- Email info@NeoSoft.com for info on getting interactive Internet access.
- "In a minute, I'll burp up your droid."
-