Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter - How to create a combo box with autocompletion

Is it possible to create a combobox that updates with the closest item in its list as you type into it?

For example:

A = ttk.Combobox()
A['values'] = ['Chris', 'Jane', 'Ben', 'Megan']

Then you type "Chr" into the combobox, I want it to automatically fill in "Chris".

like image 463
TheBeardedBerry Avatar asked Sep 06 '12 10:09

TheBeardedBerry


People also ask

How do I use TTK combobox?

Use ttk. Combobox(root, textvariable) to create a combobox. Set the state property to readonly to prevent users from entering custom values. A combobox widget emits the '<<ComboboxSelected>>' event when the selected value changes.

What is Textvariable in tkinter combobox?

textvariable. A variable linked to the current value of the combobox; when the variable is changed, the current value of the combobox will be updated, while when the user changes the combobox, the variable will be updated. values.

Does tkinter have a combobox?

The Tkinter Combobox is one of the UI widgets in the Tkinter library and it is used for to select the values in the drop-down list column also it is one of the combinations of Entry and drop-down widgets these drop-down entries are replaceable one and the user if suppose not select the value in the box it automatically ...

What is Tk () in tkinter programming?

The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.


1 Answers

Yes, it can be easily done using the following example, taken from here:

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()
like image 126
Elyasaf755 Avatar answered Sep 17 '22 15:09

Elyasaf755