Posted to tcl by jeremy_c at Fri Feb 05 13:22:46 GMT 2010view raw

  1. package require Tk
  2.  
  3. namespace eval ttk::combobox {
  4. proc CompleteEntry {path key} {
  5. if {[string length $key] > 1 && [string tolower $key] != $key} {return}
  6.  
  7. set values [$path cget -values]
  8. set x [lsearch -nocase $values $key*]
  9. if {$x < 0} {return}
  10.  
  11. set index [$path index insert]
  12. $path set [lindex $values $x]
  13. $path icursor $index
  14. $path selection range insert end
  15. }
  16.  
  17. proc CompleteList {W key} {
  18. for {set idx 0} {$idx < [$W size]} {incr idx} {
  19. if {[string match -nocase $key* [$W get $idx]]} {
  20. $W selection clear 0 end
  21. $W selection set $idx
  22. $W activate $idx
  23. break
  24. }
  25. }
  26. }
  27. }
  28.  
  29. bind ComboboxListbox <KeyPress> { ttk::combobox::CompleteList %W %K }
  30. bind TCombobox <KeyPress> { ttk::combobox::CompleteEntry %W %K }
  31.  
  32. set values {One Two Three Four Five Six Seven Eight Nine Ten}
  33.  
  34. ttk::label .l1 -text Readonly
  35. ttk::combobox .c1 -state readonly -values $values
  36. ttk::label .l2 -text Editable
  37. ttk::combobox .c2 -values $values
  38.  
  39. pack .l1 .c1 .l2 .c2