Re: Using foreach loop to create radiobutton menu



aj@xxxxxxxxxx wrote:
I am trying to do the same thing in a foreach loop. I keep getting an
error though saying variable range does not exists: Here is that
code...

menubutton range -text "Set Range" -underline 0 -bd 0 -menu {
foreach range {1 2 3 4 5 6 7 8 9 10} {
radiobutton range$range -label $range -command {
puts "hello: $range"
}
}
}

I understand that range is out of scope. Can anyone tell me how to
make it so it is in scope? I tried using upvar and uplevel to no
avail.


This is just a simple quoting problem.

First off, answer the question "is range a local or global variable?". Think about how the answer applies to when the radiobutton is created versus when the code associated with the radiobutton actually runs.

Then, when you define the value for the -command option, think about what quotes you are using and what effect that has on variable subsitution. It also helps to think about the difference between when you create the widget and when the code actually runs.

The short version is, don't use curly braces if you need a variable to be expanded. A longer version is here:

http://www.tclscripting.com/articles/jan06/article3.html



--
Bryan Oakley
http://www.tclscripting.com
.