使用跟踪时获取 porper 列表框项目

get porper listbox item when using trace

...
import tkinter
from tkinter import StringVar


adfl = ['Alan Alexander Milne', 'Alice Hoffman', 'Alicia Bay Laurel', 'Alison Weir',       'Alistair Cooke','Alycea Ungaro', 'Amanda Quick', 'Ann Durell', 'Anne De Courcy', 'Anne Kent Rush', 'Anne McCaffrey','Anne Purdy', 'Anne Rice', 'Anon', 'Antoine de Saint-Exupery', 'Anya Seton', 'Arthur Conan Doyle','Ashida Kim', 'Aubrey Beardsley', 'BBC', 'Barbara Ann Brennan', 'Barbara Walker', 'Bertrice Small','Betsy Bruce', 'C. S. Lewis', 'Caitlin Matthews', 'Carl Sagan', 'Carol Belanger Grafton', 'Carol Blackman','Carol Kisner', 'Caroline Foley', 'Carolyn Kisner', 'Catherine Coulter', 'Charles Greenstreet Addison','Charlotte Bronte', 'Chic Tabatha Cicero', 'Christina Dodd', 'Christopher Paolini', 'Clare Maxwell-Hudson','Clarissa Pinkola Estés', 'Co Spinhoven', 'D. J. Conway', 'D.H. Lawrence', 'Dan Brown','Daniel M. Mendelowitz', 'Deborah E. Harkness', 'Denise Dumars', 'Denys Hay', 'Diana Gabaldon','Diana L. Paxson', 'Dinah Lovett', 'Dion Fortune', 'Donald M. Anderson']


def update_list(*args):
    frame1_lb.delete(0, 'end')
    search_term = ent_var.get()
    for item in adfl:
        if search_term.lower() in item.lower():
            frame1_lb.insert('end', item)
    return


def author_list():
    # Clear entry box
    ent_var.set("")
    frame1.configure(text='Author')
    frame1_list.set(adfl)
    # Set up trace for list update, only need this one instance to make work
    ent_var.trace("w", update_list)
    frame1_lb.bind('<<ListboxSelect>>', sauthor_list)


def sauthor_list(self):
    caut = frame1_lb.curselection()
    print(caut)
    saut = adfl[caut[0]]
    print(saut)


##########
window = tkinter.Tk()
window.geometry("600x900")
window.resizable(width=False, height=False)
window.wm_title("My Book Library")
window.configure(bg='#5e84d4')
window.update_idletasks()
# Gets the requested values of the height and widht.
windowWidth = window.winfo_width()
windowHeight = window.winfo_height()
# Gets both half the screen width/height and window width/height
positionRight = int((window.winfo_screenwidth() / 2) - windowWidth / 2)
positionDown = int((window.winfo_screenheight() / 2) - windowHeight / 2)
# Positions the window in the center of the page.
window.geometry("+{}+{}".format(positionRight, positionDown))
# Layout of frames

frame1 = tkinter.LabelFrame(window, text='', bg='lightblue')
frame1.place(relx=0.010, rely=0.10, relheight=0.890, relwidth=0.500)
frame1.configure(relief='groove')
frame1.configure(borderwidth="2")
frame1_list = StringVar()
frame1_lb = tkinter.Listbox(frame1, listvariable=frame1_list, width=40, height=44)
frame1_lb.place(x=0.0, y=0.30)
frame1_sb = tkinter.Scrollbar(frame1, orient=tkinter.VERTICAL)
frame1_lb.config(yscrollcommand=frame1_sb.set, bg='white')
frame1_sb.pack(side=tkinter.RIGHT, fill=tkinter.Y)
frame1_sb.config(command=frame1_lb.yview)

frame1a = tkinter.LabelFrame(window, text="Enter letters  - for search")
frame1a.configure(border=2, relief='groove')
frame1a.place(relx=0.010, rely=0.05, relheight=0.05, relwidth=0.500)
ent_var = StringVar()
frame1_ent = tkinter.Entry(frame1a, textvariable=ent_var,  width=40, bg='white')
frame1_ent.place(x=0.0, rely=0.0)
search_term = ent_var.get()


author_list()
window.mainloop()
...

所以,我可以使用跟踪通过输入输入框来缩短我想要的列表。但是当我点击缩短的列表时,我得到的是前一个列表中的位置值,而不是新的缩短列表值。在不使用输入框的情况下工作正常,以缩短列表。 这是我对自动完成列表框类型的尝试。

您使用了错误的来源来显示所选项目。由于缩短列表与 adfl 不同,因此不应在 sauthor_list() 中使用 adfl。您需要使用 frame1_lb.get() 来获取所选项目:

def sauthor_list(self):
    caut = frame1_lb.curselection()
    print(caut)
    saut = frame1_lb.get(caut[0])  # get the selected item
    print(saut)