I am trying to find a way to search items in list with stars (***
) which replace missing letter. I want to allow user to search for medications in list like this (a*u*g*t*n*
) so that he can get in list (augmentin susp ) and all possible medications containing these search letters.
If user forgets any letter he can replace it with stars in search entry to display all possible items in list containing these letters. Here is my simple code.
I tried to use re
but i failed and can't use it in a proper way.
User can write in search entry for example (a*u*g*m*n*
) and press enter, then list will show every possible item containing these 4 letters.
from tkinter import *
import tkinter.font as TKFont
import customtkinter
from tkinter import messagebox
from PIL import Image , ImageTk
root = customtkinter.CTk()
root.geometry('1000x500')
def update(data):
my_list.delete(0 , END)
for item in data:
my_list.insert(END, item)
def check(event):
typed = search_entry.get()
if typed == '':
data = medications
else:
data = []
for item in medications:
if typed.lower() in item.lower():
data.append(item)
update(data)
my_list=Listbox(root , bg='#2C2C2C' , fg='white')
my_list.place(relx=.1 , rely=.02)
search_entry=customtkinter.CTkEntry(root)
search_entry.place(relx=.3 , rely=.02)
search_entry.bind('<Return>',check)
medications=['augmentin Susp' , 'Curam Susp' , 'Megamox Susp' , 'Hibiotic Susp' , 'Clavimox Susp' ,'E-Mox Clav Susp' , 'Averobios Susp', 'DeltaClav Susp' ,"Delatrol tab"]
root.mainloop()
CodePudding user response:
You can use fnmatch.fnmatch()
which provides support for Unix shell-style wildcards matching:
from fnmatch import fnmatch
...
def check(event):
typed = search_entry.get()
if typed == '':
data = medications
else:
data = []
for item in medications:
if fnmatch(item, typed):
data.append(item)
update(data)