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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have many checkboxes so I create

Status
Not open for further replies.

JohnMk

Programmer
Aug 30, 2002
3
RO
I have many checkboxes so I create them in a for statement:


set nck 0
foreach lf $lfiles {
checkbutton $tfl.ck_$nck -text $lf -variable vck_$nck
$tfl window create end -window $tfl.ck_$nck -align top
$tfl insert end \n

set nck [incr nck]
}


where tfl is a text widget

This actually works, but when I try to get the vars values
i get a small, but annoying problem. E.g. I try to print checkboxes vars values:


for (set i 0} {$i<$nck} {incr i} {
puts $vck_$i # this is wrong because now
# there are 2 vars: vck_ and i

}


I can't do this either:

set tempvar vck_$i
puts $tempvar

because tempvar's value is a kind of pointer.
Any idea how I could get vck_$i var value?
Or some other way I could declare a checkbutton variable..

Thanks in advance
 
Well, in answer to your direct question, the way to get the value will be to force Tcl to perform 2 rounds of variable substitution. The first round we'll get using &quot;$&quot; substitution. The second round we'll get by giving the resulting variable name to set. When set receives only one argument, it simply returns the current value of that variable.

So, what you could do would be:

Code:
for (set i 0} {$i < $nck} {incr i} {
   puts [set vck_$i]
}

But there is a better approach to take. What you're in essence doing is simulating an array. Instead of simulating an array, it's better to actually use an array.

This works because the various -variable style widget options allow you to use either a global scalar variable or an element in a global array to hold the value. Here's a quick example:

Code:
for {set i 0} {$i < 10} {incr i} {
    checkbutton .cb$i -text &quot;Item $i&quot;         -variable values($i)
    pack .cb$i -padx 2 -pady 2
}

Now as you manipulate your checkbuttons, Tcl updates their corresponding elements in the global values array. You can retrieve the value of a single checkbox's state easily with standard array syntax. For example:

[tt]% for {set i 0} {$i < 10} {incr i} {
> puts &quot;values($i) = $values($i)&quot;
> }
values(0) = 1
values(1) = 0
values(2) = 0
values(3) = 1
values(4) = 1
values(5) = 0
values(6) = 1
values(7) = 0
values(8) = 0
values(9) = 0[/tt]

By the way, I also noted the following line in your code sample:

Code:
set nck [incr nck]

This is redundant. The incr command actually modifies the value of the variable. So you can get by simply with:

Code:
incr nck
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
OK, thanks.. it's a good ideea...

Meantime i found &quot;upvar&quot; command which does exactly what I need: upload in a var the value of another.
So now I have something like this:
for {set i 0} {$i<$nck} {incr i} {
set ivck [$tfl.ck_$i cget -variable]
upvar $ivck myvar
... do something with myvar ...

}


Anyway, thank you for your idea
 
&quot;upload in a var the value of another&quot;.

This is not exactly what 'upvar' does.

From the manual:
&quot;This command arranges for one or more local variables in the current procedure to refer to variables in an enclosing procedure call or to global variables.&quot;

That is, modifying myvar *IS* modifying the variable named $ivck.

ulis
 
The point is that I got the value I needed. I'm not modifying myvar I only interogate it...
I'm new to tcl so I'm learning as I program.
Anyway it's good to know..
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top