Python Tkinter Autocomplete

In this Python tutorial, we see how to implement Python Tkinter autocomplete functionality. Autocomplete refers to displaying relevant words depending on the keyword typed by the user. The easiest example could be the auto-suggest points you see while browsing.

Let’s begin.

Autocomplete Python Tkinter

Python Tkinter provides a module ttkwidgets, using which we can add an autocomplete feature in our application in Python Tkinter.

  • ttkwidgets has a method, autocomplete, which allows the developer to easily implement the autocomplete feature in their respective application.
  • The first step in the process is to install Autocomplete on the system. Windows and Mac users can simply use pip to install autocomplete, whereas Linux users have to install the complete package to use it.
  • ttkwidgets is dependent on pillow, so we need to install that as well on the system to make this ttkwidgets library work.

Windows and Mac users:

pip install ttkwidgets
pip install pillow

or

pip3 install ttkwidgets
pip3 install pillow

For Linux, we will be using Ubuntu, which has apt as a package manager. You can change apt to your respective package manager. Arch Linux users, click here.

sudo add-apt-repository ppa:j-4321-i/ttkwidgets

sudo apt-get update

sudo apt-get install python3-ttkwidgets

sudo apt-get install python-imaging-tk

Once you have executed all these commands successfully, you can follow the steps in the sections below to implement the autocomplete feature in Python Tkinter.

Read Create Animations in Python with Tkinter

Autocomplete Entry Tkinter Python

In this section, we will learn how to autocomplete the Entry widget in Python Tkinter. Also, we will see an example of it.

  • The first step in the process is to import the AutocompleteEntry method from ttkwidgets.autocomplete
  • Now, instead of creating an Entry widget directly from Tkinter, we will create it using AutocompleteEntry, and inside it, we will pass a parameter for completevalues.
  • The values passed in the completevalues parameter will become options that will be completed when the user passes similar keywords.
  • For example, completevalues=['hello', 'PythonGuides'], every time a user types ‘h ‘, he/she will see autocomplete suggestion as ‘hello’; similarly, in the case of ‘p’, they will see ‘pythonguides’ as an option.

Source code of Autocomplete Entry in Tkinter Python

from ttkwidgets.autocomplete import AutocompleteEntry
from tkinter import *

countries = [
        'Antigua and Barbuda', 'Bahamas','Barbados','Belize', 'Canada',
        'Costa Rica ', 'Cuba', 'Dominica', 'Dominican Republic', 'El Salvador ',
        'Grenada', 'Guatemala ', 'Haiti', 'Honduras ', 'Jamaica', 'Mexico',
        'Nicaragua', 'Saint Kitts and Nevis', 'Panama ', 'Saint Lucia', 
        'Saint Vincent and the Grenadines', 'Trinidad and Tobago', 'United States of America'
        ]

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#f25252')

frame = Frame(ws, bg='#f25252')
frame.pack(expand=True)

Label(
    frame, 
    bg='#f25252',
    font = ('Times',21),
    text='Countries in North America '
    ).pack()

entry = AutocompleteEntry(
    frame, 
    width=30, 
    font=('Times', 18),
    completevalues=countries
    )
entry.pack()

ws.mainloop()

Output of Autocomplete Entry in Python Tkinter

Here is the output of the above code for Autocomplete Entry in Python Tkinter. You can notice that when a user typed ‘Un‘, it automatically completed with the country name to ‘United States of America’.

python tkinter autocomplete entry

Check out Master Python Tkinter Events

Python Tkinter Autocomplete Combobox

In this section, we will learn how to autocomplete the Combobox widget in Python Tkinter.

  • The first step in the process is to import the AutocompleteEntry method from ttkwidgets.autocomplete
  • Now, instead of creating a Combobutton or Combobox widget directly from Tkinter, we will create it using AutocompleteCombobox, and inside it, we will pass a parameter for completevalues.
  • The values passed in the completevalues parameter will become options that will be completed when the user passes similar keywords.

Source code of Autocomplete combobox in Tkinter Python

from ttkwidgets.autocomplete import AutocompleteCombobox
from tkinter import *

countries = [
        'Antigua and Barbuda', 'Bahamas','Barbados','Belize', 'Canada',
        'Costa Rica ', 'Cuba', 'Dominica', 'Dominican Republic', 'El Salvador ',
        'Grenada', 'Guatemala ', 'Haiti', 'Honduras ', 'Jamaica', 'Mexico',
        'Nicaragua', 'Saint Kitts and Nevis', 'Panama ', 'Saint Lucia', 
        'Saint Vincent and the Grenadines', 'Trinidad and Tobago', 'United States of America'
        ]

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#8DBF5A')

frame = Frame(ws, bg='#8DBF5A')
frame.pack(expand=True)

Label(
    frame, 
    bg='#8DBF5A',
    font = ('Times',21),
    text='Countries in North America '
    ).pack()

entry = AutocompleteCombobox(
    frame, 
    width=30, 
    font=('Times', 18),
    completevalues=countries
    )
entry.pack()

ws.mainloop()   

Source code of Autocomplete combobox in Tkinter Python

Here is the output of the above code for Autocomplete Combobox in Python Tkinter. You can notice that when a user typed ‘Un‘, it automatically completed the text in the Entry box and highlighted the same in the dropdown menu with the country name to ‘United States of America’.

python- kinter autocomplete combo

Read: Extract text from a PDF using Python

Read Create Tabbed Interfaces in Python with the Tkinter Notebook Widget

Python Tkinter Autocomplete Listbox

  • The first step in the process is to import the AutocompleteEntryListbox method from ttkwidgets.autocomplete.
  • Now, instead of creating a Listbox widget directly from Tkinter, we will create it using AutocompleteEntryListbox, and inside it, we will pass a parameter for completevalues.
  • The values passed in the completevalues parameter will become options that will be completed when the user passes similar keywords.

Source code of Autocomplete Listbox in Tkinter Python

from ttkwidgets.autocomplete import AutocompleteEntryListbox
from tkinter import *

countries = [
        'Antigua and Barbuda', 'Bahamas','Barbados','Belize', 'Canada',
        'Costa Rica ', 'Cuba', 'Dominica', 'Dominican Republic', 'El Salvador ',
        'Grenada', 'Guatemala ', 'Haiti', 'Honduras ', 'Jamaica', 'Mexico',
        'Nicaragua', 'Saint Kitts and Nevis', 'Panama ', 'Saint Lucia', 
        'Saint Vincent and the Grenadines', 'Trinidad and Tobago', 'United States of America'
        ]

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#DFE7F2')

frame = Frame(ws, bg='#DFE7F2')
frame.pack(expand=True)

Label(
    frame, 
    bg='#DFE7F2',
    font = ('Times',21),
    text='Countries in North America '
    ).pack()

entry = AutocompleteEntryListbox(
    frame, 
    width=30, 
    font=('Times', 18),
    completevalues=countries
    )
entry.pack()

ws.mainloop()   

Source code of Autocomplete Listbox in Tkinter Python:

Here is the output of the above code for Autocomplete Listbox in Python Tkinter. You can notice that when a user selects Canada, it automatically updates in the Entry box.

python tkinter autocomplete listbox

In this article, I have explained Python Tkinter autocomplete. I discussed autocomplete in Python Tkinter, autocomplete entry in Python Tkinter, autocomplete combobox in Python Tkinter, and autocomplete listbox in Python Tkinter.

You may like the following Python Tkinter tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.