Answer the question
In order to leave comments, you need to log in
When calling an event, bind('') does not take into account the value of the click button, how to fix it?
I am studying and the questions are probably stupid)
Given:
There is a pre-written list.
Entry input field
created Listbox display field created
When the user enters data in the input field, the listbox should display values from the list that match the entered text.
Problem:
data is taken into account with a delay of 1 character, that is, the character that triggers the search and display function is not taken into account
below code
pril=['hdhd','pertte','etewt','ertwew','vvviviv']
def pril_poisk (event):
lbox.delete(0, END)
b=[]
temp=e.get()
print(temp)
for i in range(len(pril)):
a=pril[i].find(temp)
if a!=-1:
lbox.insert(END, pril[i])
else:
pass
#enter application items
e=Entry(window, width=15)
e.grid( row=4, column=0)
e.bind('', pril_poisk)
lbox=Listbox(window, width=25, selectmode=EXTENDED)
lbox.grid(row=7, column=0)>
Answer the question
In order to leave comments, you need to log in
Since no one answered, I myself came up with such a solution to
make a handler that will catch char from the input character and add it to the search accordingly, since the input field is only in Russian, it turned out like this:
alf=['я','ч','с','м','и','т','ь','б','ю','ф','ы','в','а','п','р','о','л','д','ж','э','й','ц','у','к','е','н','г','ш','щ','з','х','ъ','.','1','2','3','4','5','6','7','8','9','0']
numb={'keycode=190':'.','keycode=191':'.','keycode=49':'1','keycode=50':'2','keycode=51':'3','keycode=52':'4','keycode=53':'5','keycode=54':'6','keycode=55':'7','keycode=56':'8','keycode=57':'9','keycode=48':'0'}
#чтобы не терять первый символ ввода(последний в строке)
def obrabotka_sobitija(event):
temp1=str(event)
temp2=temp1.split(' ')
temp3=temp2[4]
temp1=temp3.replace('char=','')
temp3=temp1.replace("'",'')
try:
if temp3 not in alf:
temp3=numb[temp3]
except:
pass
print(event,'\n',temp3)
return temp3
def pril_poisk(event, lbActiv, lbPass, entr, spis):
last_key=obrabotka_sobitija(event)
lbActiv.delete(0, END)
lbPass.delete(0,END)
global b
b=[]
if last_key in alf:
temp=entr.get()+first_key
else:
temp=entr.get()
print(temp)
for i in range(len(spis)):
a=spis[i].find(temp)
if a!=-1:
b.append(spis[i])
lbActiv.insert(END, spis[i])
else:
pass
I think you need to check if the text in the Entry matches the list when the user writes to the Entry.
To check that in Entry it is necessary to check after each keystroke. To get the text from the Entry you need to use: name entry.get()
Well, then:
if название entry.get() in список:
добавить в Listbox
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question