Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tk Variable quandry 1

Status
Not open for further replies.

rubberscissors

Technical User
Oct 11, 2001
21
US
I have an instance where I generate a series of radio buttons in a loop, which use the variable x$c (where c is the current iteration of the loop, so I get x1, x2, x3, etc)...later, I need to call this variable in an if statement if { $x$c == $y }...when I do this, I get an error 'can't read $x$c: no such variable'. I changed the original variable to x($c), but then all I get is 'can't read x(1), no such variable'...I thought variables associated with buttons were global? Does anyone know what I'm doing wrong?
 
Replace $x$c by ${x}$c in the if statement.

ulis
 
Ok I tried that...now it's giving me the error:

Syntax error in expression ${x}$c == 1

Did I do that right? You said { } right, not ( )?

 
That's a somewhat odd little construct you've got there. Personally, I'd use an array (which is basically what you're trying to do there) rather than construct variable names like that on the fly.

The easiest way to solve your problem as presented, though, is to explicitly call the set command to retrieve the value of the variable. Calling set with only one argument returns the value of the variable. Thus, you could say:

Code:
if { [set x$c] == $y } {...}
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
I tried that, and also I tried it as an array $x($c) but it still doesn't work...it claims that it doesn't know anything about that variable...

The way I'm creating these is to say

while { $c <= 32 } {

radiobutton .node$c.type1010 -variable x($c) -relief raised -borderwidth 1 -text &quot;1010&quot; -value 1 -command update

}

frame .node$c are created earlier in the loop. This creates 32 rows of buttons (32 is the max number, so I create all instances off the bat). Now, I want to pack the number defined by the user, which is variable n (how many rows should actually be packed). This number is entered in an entry box, and another button called 'update' calls up the update proceedure...it figures out how many rows to pack, then tries to determine which radiobutton was clicked in each row by looking at the value assigned to x($c). Since different options are displayed depending on which radiobutton is selected, I have it cycle through and check them whenever 'Update' is clicked, and also whenever a radiobutton is clicked.

Update says (remember, n is the number of rows):

set z 1
while { $z <= n } {

if { $x($z) == 1 } { set total 2
} elseif { $x($z) == 2 } { set total 2
} elseif { $x($z) == 3 } { set total 3
} else { set total 2 }

}

But everytime I do this, it says it doesn't know about x(1) ...if I use $x($c), it says it doesn't know about c (no such variable)

I thought button variables were global? Since $x($c) is defined when the radiobutton is clicked, why can't the command which is also called by the radiobutton recognize the variable?




 
Let's see... You've got a couple of potential problems areas, if I understand your summary correctly.

You wrote: &quot;I thought button variables were global?&quot; More or less. The variables you use for the -variable attributes must be the name of global variables. When you click the button, the -value you specified for that radiobutton is assigned to the global variable; if the variable doesn't already exist, Tcl creates it in the global scope.

However, you also wrote: &quot;Since $x($c) is defined when the radiobutton is clicked, why can't the command which is also called by the radiobutton recognize the variable?&quot;

In Tcl all callbacks (code associated with -command attributes, timer events registered with the after command, handlers registered with the fileevent command, etc.) are executed in the global scope. Doesn't sound like a problem, since it sounds as though the x array variable we're talking about seems to be in the global scope.

However, if your callback consists of calling a procedure, then the procedure executes in its own local scope. And to access global variables from within a procedure, you have to use the global command within the procedure. In other words, you'd need something like this:

Code:
radiobutton .node$c.type1010     -variable x($c) -value 1 -command update

proc update {} {
    # We're now in a scope local
    # to the update procedure

    global x

    # Etc...
}

Which brings up another potential problem. If you've used &quot;update&quot; as the name of a procedure, change it. Tcl has a built-in update command, which is used to process any pending events and refresh the screen. Tcl doesn't &quot;protect&quot; built-in procedures; it's quite possible to overwrite or redefine them. Usually, it's a bad idea unless you really know what you're doing.
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
That did it...many thanks! I really appreciate you taking the time to walk me through that, I don't have any formal Tk/Tcl training and I'd been messing with that problem for quite a while! Thanks a bunch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top