In this Python tutorial, we see how to implement Python Tkinter autocomplete functionality. Autocomplete refers to displaying relevant words depending upon the keyword typed by the user. The easiest example could be the auto-suggest points you see while browsing. We will cover these topics.
- Autocomplete Python Tkinter
- Autocomplete Entry Tkinter Python
- Python Tkinter Autocomplete Combobox
- Python Tkinter Autocomplete Listbox
Autocomplete Python Tkinter
Python Tkinter provides a module ttkwidgets using which we can add autocomplete feature in our application in Python Tkinter.
- ttkwidgets have method autocomplete which allows the developer to easily implement 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 the 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 an apt as 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 below sections to implement the autocomplete feature in Python Tkinter.
Read: Registration form in Python using 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 using AutocompleteEntry, and inside it, we will pass a parameter for completevalues.
- The values passed in completevalues parameter will become options that will be completed when the user will pass similar keywords.
- for example,
completevalues=['hello', 'PythonGuides']
, now every time user will type ‘h; he/she will see autocomplete suggestion as ‘hello’, similarly in the case of ‘p’ he/she 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’.
Read: Python NumPy matrix
Python Tkinter Autocomplete Combobox
In this section, we will learn how to do autocomplete 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 using AutocompleteCombobox, and inside it, we will pass a parameter for completevalues.
- The values passed in completevalues parameter will become options that will be completed when the user will pass 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’.
Read: Extract text from PDF Python
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 using AutocompleteEntryListbox, and inside it, we will pass a parameter for completevalues.
- The values passed in completevalues parameter will become options that will be completed when the user will pass 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-selected Canada it automatically updated in the Entry box.
You may like the following Python Tkinter tutorials:
- Python Tkinter Mainloop with Examples
- Python Tkinter Scrollbar
- Python Tkinter Text Box Widget
- Python Tkinter Grid
- Python Tkinter Spinbox
- Python Tkinter OptionMenu
In this tutorial, we learned how to create autocomplete in Python Tkinter. Also, we have covered these topics.
- Autocomplete Python Tkinter
- Autocomplete Entry Tkinter Python
- Python Tkinter Autocomplete Combobox
- Python Tkinter Autocomplete Listbox
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.