繁体   English   中英

我想要两个列表框来显示彼此对应的项目

[英]I want two list boxes to show items that correspond to each other

我有两个列表框,一个有文字,另一个有数字。 在我用作示例的代码中,我对其进行了简化,以便第一个列表框具有从 a 到 j 的字母列表,然后重复此列表,但这些字母旁边有一个数字。 第二个列表框只有数字。 现在我希望能够键入“a”,它会显示列表 1 中以“a”开头的项目,然后在第二个列表框中显示与列表框 1 中的项目相对应的数字。例如,a应该有1对应,b应该有2,依此类推。 我已经想出如何让第一个列表框显示所有以我键入的字母开头的项目,但我不知道如何让第二个列表框显示 go 与这些项目的数字。

from tkinter import *

win = Tk()
win.title("test")
win.geometry("1000x600")
win.resizable(False, False)

dummylist2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18'
    , '19', '20']
dummylist1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'a2', 'b1', 'c5', 'd3', 'e8', 'f9', 'g10', 'h4'
    , 'i6', 'j7']


searchcounter = 0
entercounter = 0


def characterpressedfunction(e):  # sets up the function of the b button when it is pressed
    nlist = list(filter(lambda x: x.lower().startswith(nsearchentry.get()),
                        dummylist1))  # removes all words that don't start with the
    #  character that the user typed in
    alphabetlist.delete(0, END)  # clears the alphabetlist
    for item in nlist:  # loops though the list
        alphabetlist.insert(END, item)  # moves each individual item from the list into the listbox
        # as the list gets looped


alphabetlist = Listbox(win, width=20, font=('Arial', 15))
numberlist = Listbox(win, width=20, font=('Arial', 15))

alphabetlist.delete(0, END)  # clears the alphabetlist
for item in dummylist1:  # loops though the list
    alphabetlist.insert(END, item)  # moves each individual item from the list into the listbox
    # as the list gets looped


numberlist.delete(0, END)  # clears the alphabetlist
for item in dummylist2:  # loops though the list
    numberlist.insert(END, item)  # moves each individual item from the list into the listbox
    # as the list gets looped


nsearchentry = Entry(win, width=2, font=('Arial', 15))
nsearchentry.bind("<KeyRelease>", characterpressedfunction)


numberlist.place(relx=.4, rely=.2)
alphabetlist.place(relx=.1, rely=.2)
nsearchentry.place(relx=.3, rely=.7)

win.mainloop()

您可以使nlist成为元组 (alpha, number) 的列表,如下所示:

def characterpressedfunction(e):
    srch = nsearchentry.get()
    nlist = list(filter(lambda x: x[0].lower().startswith(srch), zip(dummylist1, dummylist2)))
    alphabetlist.delete(0, END)
    numberlist.delete(0, END)
    for a,n in nlist:  # loops through the filtered list
        alphabetlist.insert(END, a)
        numberlist.insert(END, n)

请注意,我更喜欢使用列表理解:

srch = nsearchentry.get()
nlist = [(a,n) for a,n in zip(dummylist1, dummylist2) if a.lower().startswith(srch)]

或者甚至删除nlist的创建并只使用 for 循环:

def characterpressedfunction(e):
    alphabetlist.delete(0, END)
    numberlist.delete(0, END)
    srch = nsearchentry.get()
    for a,n in zip(dummylist1, dummylist2):
        if a.lower().startswith(srch):
            alphabetlist.insert(END, a)
            numberlist.insert(END, n)

参考官方文档zip()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM