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

need help with dynamic tcl displays 1

Status
Not open for further replies.

sevans141

Technical User
Oct 24, 2008
6
0
0
US
I'm fairly new to tcl scripting and could use a little help. I have a simple list file that will be of unknown size (somewhere between 10 to 20 names). I'd like to create a gui that has a checkbutton for each name in the list and a single action button that will do something for all the checkboxes that have been selected.

Here's what I have as well as what I need...

-----------------------------------------------------------

frame .one
foreach person {some way to read the list file and create the list here}
set lower [string tolower $person]
checkbutton .one.$lower -text $person -variable $person
pack .one.$lower -side top -anchor w
}
pack .one -side top

frame .action
pack .action -side bottom
button .action.do_it -text "Do it" -command do_it
button .action.quit -text "Quit" -command quit
pack .action.do_it .action.quit -side left

proc do_it {} {
foreach person {same list as above}
set lower [string tolower $person]
if {variable for check box for this person}
puts "$person was selected. Some action will be taken"
exec some action will be taken here
}
}
}
-----------------------------------------------------------

- I need some way to read in the file and populate the foreach list.

- I also need to be able to determine it a checkbox has been selected and take some action.





I'll take any help or suggestions you can offer.



Thanks in advance...
 
First, let's look at reading the file into a list (dead easy by the way). It could be that each line of the file has several pieces of information, only one of which (the name) is pertinent. From your post, I believe this is not the case. If it is this code will have to change slightly:
Code:
set fid [open [red]<your file name>[/red] r]
set prsnLst [split [read $fid] \n]
close $fid

Ok. Now each line of the file is an element of the list, "prsnLst". Now you want to know how to query the condition of a checkbutton. Since you didn't specify otherwise, the value of the variable you assigned to each checkbutton ($person) will be '1' when it's checked, '0' otherwise. So you'll need to run through your list again, creating the variables and checking them as booleans. Now, there may be better ways to do this but this works:
Code:
if {[set [subst $person]]} {
   [red]your favorite action here[/red]
}


_________________
Bob Rashkin
 
Oh. You'll probably need to refer to the global scope of the variables associated with the checkbuttons in the proc:
if {[set ::[subst $::person]]}

_________________
Bob Rashkin
 
between the time I posted and getting your response, I figured out how to read i n the list file. But I appreciate your help anyway.

The good news is that the display is presented, it's correct, and each person has a checkbox by their name.

But... When I select a few names and hit "Do it" I get the following...

Error: can't read "::Danny": no such variable

'Danny' happens to be the last item in the list. But I get this error under all conditions (i.e. all names are selected, no names are selected, Danny is selected, Danny is not selected, etc.)

Any thoughts?


Scott E.


 
Since your variables should be lowercase, I'd have to suspect that you're not doing something you say you are doing. Maybe you need to post more code?

_________________
Bob Rashkin
 
Here's what I have now...

set myFile [open namefile r]
set namelist [split [read $myFile] \n]
close $myFile

frame .one
foreach person $name_list {
set lower [string tolower $person]
checkbutton .one.$lower -text $person -variable $person
pack .one.$lower -side top -anchor w
}
pack .one -side top

frame .action
pack .action -side bottom
button .action.do_it -text "Do it" -command do_it
button .action.quit -text "Quit" -command quit
pack .action.do_it .action.quit -side left

proc do_it {} {
global name_list
foreach person $name_list {
if {[set ::[subst $::person]]} {
puts "$person was selected. Take some action"
}
}
}
 
I assume that "namelist" in set namelist [split [read $myFile] \n] is a typo as elsewhere you use "name_list".

I think I see now. You've made "name_list" a global variable so "person" is local in your proc.
if {[set ::[subst $[red]::[/red]person]]} {
should be
if {[set ::[subst $person]]} {

_________________
Bob Rashkin
 
BOB!!! You're my new hero. You were all over the answer like fat on a mother-in-law. You're last comment put me over the top!

I appreciate all your help and the quick responses.


Scott E.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top