Advertisement
ALEXANDAR_GEORGIEV

autocomplete_list

Jun 17th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. import tkinter as tk
  2. import re  # import regular expression library
  3. from tkinter import END
  4.  
  5. my_w = tk.Tk()
  6. my_w.geometry("410x400")  # Size of the window
  7. my_w.title("plus2net.com")  # Adding a title
  8. font1 = ('Times', 24, 'bold')  # font size and style
  9. l0 = tk.Label(text='Autocomplete', font=font1)  # adding label at top
  10. l0.grid(row=0, column=1)
  11. # data source list,
  12. my_list = ['aecde', 'adba', 'acbd', 'abcd', 'abded',
  13.            'bdbd', 'baba', 'bcbc', 'bdbd']
  14.  
  15.  
  16. def my_upd(my_widget):  # On selection of option
  17.     my_w = my_widget.widget
  18.     index = int(my_w.curselection()[0])  # position of selection
  19.     value = my_w.get(index)  # selected value
  20.     e1_str.set(value)  # set value for string variable of Entry
  21.     l1.delete(0, END)  # Delete all elements of Listbox
  22.  
  23.  
  24. def my_down(my_widget):  # down arrow is clicked
  25.     l1.focus()  # move focus to Listbox
  26.     l1.selection_set(0)  # select the first option
  27.  
  28.  
  29. e1_str = tk.StringVar()  # string variable
  30. e1 = tk.Entry(my_w, textvariable=e1_str, font=font1)  # entry
  31. e1.grid(row=1, column=1, padx=10, pady=0)
  32. # listbox
  33. l1 = tk.Listbox(my_w, height=6, font=font1, relief='flat',
  34.                 bg='SystemButtonFace', highlightcolor='SystemButtonFace')
  35. l1.grid(row=2, column=1)
  36.  
  37.  
  38. def get_data(*args):  # populate the Listbox with matching options
  39.     search_str = e1.get()  # user entered string
  40.     l1.delete(0, END)  # Delete all elements of Listbox
  41.     for element in my_list:
  42.         if (re.match(search_str, element, re.IGNORECASE)):
  43.             l1.insert(tk.END, element)  # add matching options to Listbox
  44.  
  45.  
  46. # l1.bind('<<ListboxSelect>>', my_upd)
  47. e1.bind('<Down>', my_down)  # down arrow key is pressed
  48. l1.bind('<Right>', my_upd)  # right arrow key is pressed
  49. l1.bind('<Return>', my_upd)  # return key is pressed
  50. e1_str.trace('w', get_data)  #
  51. # print(my_w['bg']) # reading background colour of window
  52. my_w.mainloop()  # Keep the window open
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement