-1

When the user starts typing into the entry field, I want the possibilities shown underneath the tkinter entry field just as if you would type into the Google search bar. The possibilities are placed in the fruits array.

import tkinter as tk

window = tk.Tk()

fruits = ["Apple", "Banana", "Peach", "Pear"]

label = tk.Label(text="Fruit")
label.grid(row=0, column=0)

entry = tk.Entry()
entry.grid(row=0, column=1)

window.mainloop()

You can find an image of what I want here: https://i.stack.imgur.com/pnozv.png (I am not allowed to post images yet)

How can I make this entry field act the same as the Google search bar?

Pascal
  • 23
  • 7

2 Answers2

3
# Import the Required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window
win= Tk()

# Update the Entry widget with the selected item in list
def check(e):
   v= entry.get()
      if v=='':
      data= values
   else:
      data=[]
      for item in values:
         if v.lower() in item.lower():
            data.append(item)
   update(data)

def update(data):
   # Clear the Combobox
   menu.delete(0, END)
   # Add values to the combobox
   for value in data:
      menu.insert(END,value)


# Add a Label widget
label= Label(win, text= "Demo Combobox Widget", font= ('Helvetica 15
bold'), background= "green3")
label.pack(padx= 10, pady= 25)

# Add a Bottom Label
text= Label(win, text="Select your prefered fruit")
text.pack(padx= 15,pady= 20)

# Create an Entry widget
entry= Entry(win, width= 35)
entry.pack()
entry.bind('<KeyRelease>',check)

# Create a Listbox widget to display the list of items
menu= Listbox(win)
menu.pack()

# Create a list of all the menu items
values= ["Apple", "Banana", "Peach", "Pear"]
# Add values to our combobox
update(values)

# Binding the combobox onclick

win.mainloop()
Modestas
  • 55
  • 5
  • Thank you for trying to help out. This is not exactly what I meant. For this example I only used 4 values but I will use a list of 60+ values. I do not want the list visible before the user starts typing. Also when the correct value is visible I would like the user to be able to click it or to use the arrow key to select and use the value. Just like the Google search bar. – Pascal Aug 17 '22 at 12:25
  • Maybe this is something you are looking for : https://stackoverflow.com/questions/55649709/is-autocomplete-search-feature-available-in-tkinter-combo-box – Modestas Aug 18 '22 at 05:54
0

When the user starts typing into the entry field, I want the possibilities shown underneath the tkinter entry field just as if you would type into the Google search bar.

How can I make this entry field act the same as the Google search bar.

The solution can be fixed.

  • Add Entry widget.
  • Add Listbox widget.
  • Create check_key() function. This will search in Listbox for what you type.
  • Add e.bind for check_key() function. We'll be using Walrus.
  • Create update() function. It will remove item when pressing the spacebar to update the Listbox.
  • Create items_selected() function. If you desire, then click the item on Listbox to update the Entry widget.
  • Add lb.bind for items_selected() function.

Snippet:

import tkinter as tk


root = tk.Tk()
root.title('Search Engines')

#fruits = ["Apple", "Banana", "Peach", "Pear"]

lst = ['san francisco weather', 'san francisco', 'san francisco giants',
        'san francisco valleyr', 'san francisco state university',
        'san francisco hotels', 'san francisco 49ers', 'san fernando',
        'san francisco mission', 'san francisco zip code']

def check_key(event: bool) -> bool:
   if (value := event.widget.get()) =='':
       data = lst
   else:
       data = [item for item in lst if value.lower() in item.lower()]
                                    
   update(data)

def update(data: str) -> None:  
    lb.delete(0, 'end')

    for item in data:
        lb.insert('end', item)

def items_selected(event: None) -> None:
    selected_langs = ",".join([lb.get(i) for i in lb.curselection()])
    e.delete(0, tk.END)
    e.insert(0, selected_langs)      

e = tk.Entry(root)
 
lb = tk.Listbox(root)

e.pack()
lb.pack()

e.bind('<KeyRelease>', check_key)
lb.bind('<<ListboxSelect>>', items_selected)

update(lst)

root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19