Reputation: 189
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)
Upvotes: 4
Views: 13740
Reputation: 59
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()
Upvotes: 4
Reputation: 1
I am using the tk.OptionMenu() widget to implement the ComboBox dropdown in my code.
With this widget, I found that if I also include an tk.Entry() widget right next to it, with both widgets sharing the same textVariable, tkinter automatically works as an autosearch in the dropdown.
Here is a short snippet
selectedItemText = tk.StringVar()
aEntry = tk.Entry(self.master, kw_tkEntry)
aEntry['textvariable'] = selectedItemText
aComboBox = tk.OptionMenu(self.master, selectedItemText, *set(largeDataList))
Hope this helps somebody
Upvotes: 0
Reputation: 491
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
Upvotes: 1
Reputation: 1429
You can try as this
from tkinter import *
from tkinter import ttk
root = Tk()
def search():
value_to_search = var.get()
if value_to_search == "" or value_to_search == " ":
dim_combo['values'] = element_names
else:
value_to_siplay = []
for value in element_names:
if value_to_search in value:
value_to_siplay.append(value)
dim_combo['values'] = value_to_siplay
element_names = list([str(a) for _ in range(100) for a in range(10)])
dim_combo = ttk.Combobox(root, state='readonly')
dim_combo['values'] = element_names
dim_combo.pack()
var=StringVar()
entry = Entry(root, textvariable=var)
entry.pack()
search_button = Button(root, text="search", command=search)
search_button.pack()
root.mainloop()
The function "search" search inside the element_names
of the Combobox for the elements that contains the string that you want to search, so if in the box we have ["hello", "mahe", "pola"]
and you search he
the checkbox will display only ["hello", "mahe"]
Upvotes: 1