Version 0 of Auto-Complete for Entry Widgets

Updated 2005-01-03 15:26:46

AJB [2005/01/03] - This is a quick little proc that uses a list to perform auto-completion in an entry box as the user enters data. Should work with other types of entry widgets as well, like the Tile combobox. Although, in the case of a combobox, this functionallity should probably be built into the widget, considering that the widget already has a list of values.

And, if you dont understand the test code, then you are probably not very cultured, and you should try to watch more classic films. ;)

 #use autocomplete in the validate command of an entry box as follows
 #  -validatecommand {autocomplete %W %d %v %P $list}
 # where list is a tcl list of values to complete the entry with
 proc autocomplete {win action validation value valuelist} {
   if {$action == 1 & $value != {} & [set pop [lsearch -inline $valuelist $value*]] != {}} {
     $win delete 0 end;  $win insert end $pop
     $win selection range [string length $value] end
     $win icursor [string length $value]
   } else {
     $win selection clear
   }
   after idle [list $win configure -validate $validation]
   return 1
 }


 #Test code--- change to if 1
 if 0 {
 package require Tk

 set fruitlist {apple bannana cherry grape grapefruit lemon loganberry mango orange \
                 {passion fruit} pear plum pomegranate prune raspberry}

 entry .test -validate all -validatecommand {autocomplete %W %d %v %P $fruitlist}
 label .top -text "Self Defense Against Fresh Fruit"
 label .ret -wraplength 300
 button .done -text Go -command {.ret configure -text "It's quite simple to defend yourself against \
                         a man armed with a [.test get].  First of all you force him to drop the [.test get].  \
                         Second, you eat the [.test get], thus disarming him.  You have now rendered him harmless."}
 grid .top -pady 5 -columnspan 2
 grid .ret -columnspan 2
 grid .test .done
 }