In this tutorial we will discuss how to implement the AutoComplete (also known as autofill) feature within the Tkinter Entry Widget. The native Tkinter implementation of the Entry widget does not directly support such a feature, hence we must turn to third party solutions, or develop our own.
Luckily, there is a convenient and easy to use third-party module we can use for this purpose called ttkwidgets
. To install this module, use the following command.
pip install ttkwidgets
AutoComplete Entry Widget
The code to create this widget is fairly simple. It functions exactly as a regular Tkinter Entry widget, but with an additional parameter called “completevalues
” into which you must pass a list of autocomplete suggestions. This Entry widget then internally calculates similarity between the currently-typed text in the Entry widget, to the list of suggestions. The suggestion with the highest similarity will be shown.
import tkinter as tk
from ttkwidgets.autocomplete import AutocompleteEntry
# Create the main window
root = tk.Tk()
# Create a list of options for the Combobox
options = ["Apple", "Banana", "Cherry", "Date", "Grapes",
"Kiwi", "Mango", "Orange", "Peach", "Pear", "Pineapple",
"Strawberries", "Blueberry"]
combobox = AutocompleteEntry(root, completevalues=options)
combobox.pack(padx=50, pady=50)
# Run the Tkinter event loop
root.mainloop()

It is not recommended to use very large word lists with this widget. If, for example you attempt to do the following, where you download a large portion of the English vocabulary using NLTK, and use it as the autocomplete suggestion list…
import tkinter as tk
from ttkwidgets.autocomplete import AutocompleteEntry
import nltk
from nltk.corpus import words
# Download the words corpus if not already downloaded
nltk.download('words')
# Get a list of English words
english_vocab = words.words()
# Create the main window
root = tk.Tk()
combobox = AutocompleteEntry(root, completevalues=dir(root))
combobox.pack(padx=50, pady=50)
# Run the Tkinter event loop
root.mainloop()
…this will backfire badly because of the lack of context-based filtering in this widget. This widget uses a very basic system to filter through the suggestions, and suggest one to you. If you give it the entire English vocab, and then type a few characters in the entry widget (e.g. “Pe”) then there will be thousands of possible matches with the “exact match” approach that this widget uses (which returns all words starting with “Pe”). Out of all the thousands of possible matches, only a few would make contextual sense.
Modern systems would involve the use of AI or similar models to infer the context, and filter out those which do not make contextual sense. Or use a model like GPT-3.5, which takes context into account when making predictions.
If this method does not appeal to you, it is not too hard to create your own custom implementation. All you need is a word list of auto-complete suggestions and a comparison algorithm (e.g. exact match, substring, AI-based) to calculate similarity between the currently typed value in the entry widget + and the list of auto-complete suggestions. The suggestion with the highest similarity should be shown. You can also implement AI models into your algorithm for better accuracy.
This marks the end of the AutoComplete Entry Widget in Tkinter Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.