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!

binding from a listbox 1

Status
Not open for further replies.

mobijay

Programmer
Dec 3, 2001
4
US
The code below creates a listbox which grabs names from a seperate file called build.txt Ive created the list box and the button but I am having trouble binding the button to the elements in the listbox. How do I set it up so that when I press the button it outputs the highlighted item in the listbox??

Here is the code:

wm title . "Automated Builds"
. config -bg grey50
label .label1 -text "Select which kit to build:"
pack .label1

proc Scroll_Set {scrollbar geoCmd offset size} {
if {$offset != 0.0 || $size != 1.0} {
eval $geoCmd;
$scrollbar set $offset $size
} else {
set manager [lindex $geoCmd 0]
$manager forget $scrollbar;
}
}

proc Scrolled_Listbox {f args} {
frame $f
listbox $f.list -xscrollcommand [list Scroll_Set $f.xscroll [list grid $f.xscroll -row 1 -column 0 -sticky we]] -yscrollcommand [list Scroll_Set $f.yscroll [list grid $f.yscroll -row 0 -column 1 -sticky ns]]
eval {$f.list configure} $args
scrollbar $f.xscroll -orient horizontal -command [list $f.list xview]
scrollbar $f.yscroll -orient vertical -command [list $f.list yview]
grid $f.list $f.yscroll -sticky news
grid $f.xscroll -sticky news
grid rowconfigure $f 0 -weight 1
grid columnconfigure $f 0 -weight 1
return $f.list
}

proc List_Select { parent values } {
frame $parent
set choices [Scrolled_Listbox $parent.choices -width 20 -height 5]
pack $parent.choices -side left -expand true -fill both
foreach x $values {
$choices insert end $x

}
}

set t [Scrolled_Listbox .f -width 40 -height 8]
pack .f -side top -fill both -expand true
set in [open "/tcl scripts/build.txt"]
while {[gets $in line] >= 0 } {
$t insert end $line
}
close $in

button .one -text "build"
pack .one -side right

Scrolled_Listbox

 
A simple matter of using a couple of Listbox operations. The Listbox curselection operation returns a list of indexes of the currently selected items. (Remember that Listbox items are numbered starting with 0!). And the Listbox get operation returns the text of an item given its index.

As you haven't configured your Listbox to allow multiple selections, get will return only zero or one indexes. (It's possible that none of the items are selected, as you've not used the Listbox selection set operation to initialize the Listbox so that an item is selected.) So, we could use the following code:

Code:
button .one -text "Build" -command {
    set index [.f.list curselection]
    if {$index != ""} {
        puts [.f.list get $index]
    }
}

The following code would work for both the single-selection Listbox and multiple-selection Listboxes:

Code:
button .one -text "Build" -command {
    foreach index [.f.list curselection] {
        puts [.f.list get $index]
    }
}
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thank you very much, The Program worked!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top