Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is autocomplete search feature available in tkinter combo box? [duplicate]

I have a tkinter combo box in which 1000s of values are there. Is it possible to have a autocomplete search feature in it?

Like if I type something in the combobox, it should perform some wildcard search and bring up the results.

            element_names = list(**a very big list**)
            dim_combo = ttk.Combobox(self, state='readonly')
            dim_combo['values'] = self.element_names
            dim_combo.place(x=100, y=100)
like image 328
user1404 Avatar asked Sep 10 '25 14:09

user1404


2 Answers

here is good solution :)

from tkinter import *

from tkinter import ttk

lst = ['C', 'C++', 'Java',
       'Python', 'Perl',
       'PHP', 'ASP', 'JS']


def check_input(event):
    value = event.widget.get()

    if value == '':
        combo_box['values'] = lst
    else:
        data = []
        for item in lst:
            if value.lower() in item.lower():
                data.append(item)

        combo_box['values'] = data


root = Tk()

# creating Combobox
combo_box = ttk.Combobox(root)
combo_box['values'] = lst
combo_box.bind('<KeyRelease>', check_input)
combo_box.pack()

root.mainloop()

like image 108
Sobhy Elgraidy Avatar answered Sep 12 '25 03:09

Sobhy Elgraidy


You can use the AutocompleteCombobox method from tkentrycomplete module. The below example can help you out.

import tkinter as tk
from tkinter import tkentrycomplete

root = tk.Tk()
box_value = tk.StringVar()

def fun():
    print(box_value.get())
combo = tkentrycomplete.AutocompleteCombobox(textvariable=box_value)
test_list = ['apple', 'banana', 'cherry', 'grapes']
combo.set_completion_list(test_list)
combo.place(x=140, y=50)
button = tk.Button(text='but', command=fun)
button.place(x=140,y=70)

root.mainloop()

You can find the module here link

like image 45
User1493 Avatar answered Sep 12 '25 03:09

User1493