Version 0 of A little list selector

Updated 2002-04-03 10:43:10

Richard Suchenwirth - Here is another mini-megawidget: two listboxes (with scrollbar), where items from the left listbox ("choices") can be copied to the right listbox by double-clicking on them. Delete an item from the right listbox ("chosen") by double clicking. The content of the "chosen" list can be retrieved at any time in a linked global variable; also you can change the "choices" by assigning to another linked list variable.

Options aren't very many yet; you can select whether a choice can be added multiply or not with the -unique {0|1} switch, and determine the background color. Reconfiguring isn't implemented either (yet) - feel free to add more bells and whistles!


  proc listbox2 {w args} {
     array set opt {-listvar "" -selvar "" -bg white -unique 1}
     array set opt $args
     frame $w
     listbox $w.0 -yscrollcommand [list $w.y0 set] -listvar $opt(-listvar)\
         -bg $opt(-bg)
     scrollbar $w.y0 -ori vert -command [list $w.0 yview]
     listbox $w.1 -yscrollcommand [list $w.y1 set] -listvar $opt(-selvar)\
         -bg $opt(-bg)
     scrollbar $w.y1 -ori vert -command [list $w.1 yview]

      if {$opt(-unique)} {
          bind $w.0 <Double-1> "ladd $opt(-selvar) \[selection get\]"
      } else {
          bind $w.0 <Double-1> "lappend $opt(-selvar) \[selection get\]"
      }
      bind $w.1 <Double-1> "lremove $opt(-selvar) \[selection get\]"

      eval grid [winfo children $w] -sticky news
      grid rowconfigure    $w 0 -weight 1
      grid columnconfigure $w 0 -weight 1
      grid columnconfigure $w 2 -weight 1
      set w
 }
 proc ladd {listName element} {
     upvar 1 $listName list
     if {[lsearch $list $element]<0} {lappend list $element}
     set list
 }
 proc lremove {listName element} {
     upvar 1 $listName list
     set pos [lsearch $list $element]
     set list [lreplace $list $pos $pos]
 }

#demo and test:

 if {[file tail [info script]] == [file tail $argv0]} {
     set testlist {foo bar grill baz and some more words}
     pack [label .0 -text "Make your selection:"]
     pack [listbox2 .x  -listvar testlist -selvar selected] -expand 1 -fill both
     bind . <Return> {puts $selected}
 }

Tk - Arts and crafts of Tcl-Tk programming