Search code examples
pythonpython-3.xtkinter

Is autocomplete search feature available in tkinter combo box?


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)

Solution

  • 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