Advertisement
here2share

# tk_autocomplete_tkinter_code.py

Dec 16th, 2022
1,451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. # tk_autocomplete_tkinter_code.py
  2.  
  3. from typing import List
  4.  
  5. def autocomplete(entry: tk.Entry, options: List[str]):
  6.     def complete(event):
  7.         # Get the current text in the entry widget
  8.         text = entry.get()
  9.        
  10.         # If there is no text, do nothing
  11.         if not text:
  12.             return
  13.  
  14.         # Get a list of matching options
  15.         matches = [option for option in options if option.startswith(text)]
  16.  
  17.         # If there is only one match, insert it into the entry
  18.         if len(matches) == 1:
  19.             entry.delete(0, tk.END)
  20.             entry.insert(0, matches[0])
  21.         # If there are multiple matches, show a list of options
  22.         elif len(matches) > 1:
  23.             # Create a listbox to display the options
  24.             listbox = tk.Listbox(entry.master, height=len(matches))
  25.             listbox.pack()
  26.  
  27.             # Insert the options into the listbox
  28.             for option in matches:
  29.                 listbox.insert(tk.END, option)
  30.  
  31.             # Set the listbox to the current position of the entry widget
  32.             entry_x, entry_y, entry_width, entry_height = entry.bbox(tk.INSERT)
  33.             listbox_x = entry_x
  34.             listbox_y = entry_y + entry_height
  35.             listbox.place(x=listbox_x, y=listbox_y)
  36.  
  37.             # Select the first item in the list
  38.             listbox.selection_set(0)
  39.  
  40.             # Bind the listbox to the key and mouse events of the entry widget
  41.             entry.bind('<KeyRelease>', update_list)
  42.             entry.bind('<Button-1>', hide_list)
  43.             listbox.bind('<Double-Button-1>', select_item)
  44.  
  45.     def update_list(event):
  46.         # Get the current text in the entry widget
  47.         text = entry.get()
  48.  
  49.         # If there is no text, delete the listbox and do nothing
  50.         if not text:
  51.             try:
  52.                 listbox.destroy()
  53.             except:
  54.                 pass
  55.             return
  56.  
  57.         # Update the list of options based on the current text
  58.         matches = [option for option in options if option.startswith(text)]
  59.  
  60.         # If there are no matches, delete the listbox and do nothing
  61.         if not matches:
  62.             try:
  63.                 listbox.destroy()
  64.             except:
  65.                 pass
  66.             return
  67.  
  68.         # Update the listbox with the new matches
  69.         listbox.delete(0, tk.END)
  70.         for option in matches:
  71.             listbox.insert(tk.END, option)
  72.  
  73.         # Select the first item in the list
  74.         listbox.selection_set(0)
  75.  
  76.     def hide_list(event):
  77.         # Hide the listbox and delete it
  78.         try:
  79.             listbox.destroy()
  80.         except:
  81.             pass
  82.  
  83.     def select_item(event):
  84.         # Insert the selected item into the entry widget
  85.         entry.delete(0, tk.END)
  86.         entry.insert(0, listbox.get(tk.ACTIVE))
  87.         # Hide the listbox and delete it
  88.         try:
  89.             listbox.destroy()
  90.         except:
  91.             pass
  92.  
  93. # Bind the autocomplete function to the key event of the entry widget
  94. entry.bind('<KeyRelease>', complete)
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement