Updated 2012-01-06 00:49:23 by RLE

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. ;)

SRIV I'm boycotting the MPAA for at least the next 40 years

AJB SRIV: That should not be a problem, Monty Python is British made. ;)
 #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 banana 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

It's safest to put the strings in the list sorted by length. For example see what happens if you
 set fruitlist [concat aaaabbb $fruitlist aaa]

and then put an a in the entry widget.


MAK A regular lsort will do that implicitly. Just use [lsort $valuelist] before lsearch.

AJB The sorting is important, however, it is also important that the list be sorted elsewhere. This script is called every time the user presses a key. If you are working with a large list, it could cause additional performance issues.

Bryan Oakley for all practical purposes I don't think there can be much of a performance issue. I can sort, for instance, 100,000 integers in about 75 milliseconds. You would have to type about 13 characters per second to get ahead of that. However, sorting on every keypress seems dubious -- unless the dataset can possibly change between keystrokes you really only have to do the sort on the first keypress, or whenever the dataset changes.

Beware This code works great. However, a lot of my $valuelist has backslashes in the strings. The autocomplete seems to stop after one is typed. Any work around for that?