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

Multiple listbox 1

Status
Not open for further replies.

VictoriaG

Programmer
Sep 10, 2003
2
US
Hi all! Do you know how to invoke information from one listbox to others, if I have three listboxes. One listbox info_1, info_2., info_3 . SO, when I click dev_1 it's suppose to outomaticle fill out others with information (first_name, last_name)

Thank you
 
As long as you're using Tcl/Tk 8.1 or later, what I'd recommend is creating a binding to your first listbox's [tt]<<ListboxSelect>>[/tt] virtual event. A listbox generates this event whenever its selection changes. In the binding action, you can then retrieve the new selection value and respond accordingly. Here's a quick example:

Code:
set empty {}
set states {California Indiana Nevada}
array set cities {
  California  {{Los Angeles} Sacramento {San Francisco}}
  Indiana     {Indianapolis Loogootee {Terre Haute}}
  Nevada      {{Carson City} {Las Vegas} Reno}
}

listbox .lb1 -listvariable states
listbox .lb2 -listvariable empty

bind .lb1 <<ListboxSelect>> {ChangeState}

proc ChangeState {} {
  global cities empty
  set index [lindex [.lb1 curselection] 0]
  if {$index != &quot;&quot;} {
    .lb2 configure       -listvariable cities([.lb1 get $index])
  } else {
    .lb2 configure -listvariable empty
  }
}

pack .lb1 .lb2 -side left -padx 4 -pady 4

- 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
 
Hello, I ran this script and I got error message
unknown option &quot;California&quot;
unknown option &quot;California&quot;
while executing
&quot;.lb2 configure -listvariable cities [.lb1 get $index] &quot;
(procedure &quot;ChangeState&quot; line 5)
invoked from within
&quot;ChangeState &quot;
(command bound to event)
So, what exactly wrong with this script??

Thank you
 
You've got a typo in your script. It looks like you had:

Code:
.lb2 configure -listvariable cities [.lb1 get $index]

What you need is:

[tt].lb2 configure -listvariable cities([ignore][.lb1 get $index][/ignore])[/tt]

In the example I gave in my original post, I was associating an element in the global array cities as the listvariable for the secondary listbox, so you have to use proper array syntax. Of course, you don't need to use arrays to implement &quot;cascading&quot; listboxes like this. It was merely a convenience for the quick example I provided.

Admittedly, I took a bit of a shortcut in embedding the get subcommand in that line. Perhaps it would have been clearer and easier to follow if I'd used an intermediate variable:

[tt][ignore]proc ChangeState {} {
global cities empty
set index [lindex [.lb1 curselection] 0]
if {$index != &quot;&quot;} {
[/ignore]set value [ignore][.lb1 get $index][/ignore]
.lb2 configure -listvariable cities($value)
} else {
.lb2 configure -listvariable empty
}
}[/tt]

- 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
 
I was try to use your example for my programm. Could you please,check my script and find some errors, I had error: can't read &quot;w&quot;: no such variable
Thank you

global PATH
set PATH/st/st_data
global DIR1
set DIR1 dir_1
cd list [glob $DIR1/*]

foreach i $list {
set direct [lindex [split $i &quot;/&quot;]1}
set first_name_stud [lindex [split $direct &quot;'&quot;] 0]
lappend first_name $first_name_stud
}
set w .student
catch {destroy $w}
toplevel $w
wm title $ &quot;Student List&quot;
wm iconname $w &quot;list&quot;

frame $w.frame2 -borderwidth .5c
pack $w.frame2 -side top -expand yes -fill y

listbox $w.frame2.list -yscroll &quot;$w.frame2.scroll set&quot;
pack $w.frame2 list -side left -expand 1 -fill both
$w.frame2.list insert end &quot;DIR1 $list&quot;
bind $w.frame2.list <<ListboxSelect>> {StudentList}

frame $w.frame3 -borderwidth .5c
pack $w.frame3 -side top -expand yes -fill y
listbox $w.frame3.list -listvariable empty -yscroll &quot;$w.frame3.scroll set&quot;
pack $w.frame3.list -side left -expand 1 -fill both


proc StudentList { } {
global first_name empty
set index [lindex [$w.frame3.list curselection] 0]
if {$index != &quot;&quot;} {
set value [$w.frame3.list get $index]
$w.frame3.list -listvariable first_name($value)
} else {
$w.frame3.list configure -listvariable empty
}
}



 
In your StudentList procedure, you try to use the global variable w without declaring it as global. As a further note on global variables, the global command outside of a procedure has no effect whatsoever; you're already in the global scope, so explicitly declaring a variable as global is redundant. You use global only within a procedure, to gain access to global variables from within the procedue.

As an additional note, in situations like these, stack traces are invaluable in helping to narrow down where the problem is. When an error occurs while running a GUI script with wish, the error dialog displayed has a &quot;Stack Trace&quot; button on it which displays the stack trace in a separate dialog. You can actually copy the stack trace from that dialog and paste it elsewhere -- like into requests for help, which helps us help you better. :) And for non-GUI scripts, you can run the troublesome script from an interactive tclsh session. When the error occurs, you can then examine the contents of the global errorInfo variable, which is where Tcl automatically stores the stack trace of the last error that occurred.

- 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
 
Thank you, another error :unknow option (DIR1 / dir_1 /Smith,John,12345678 dir_1 /Thomson,Mary,91011121....)
while executing
$w.frame3.list -listvariable first_name($value)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top