【问题标题】:I want two list boxes to show items that correspond to each other我想要两个列表框来显示彼此对应的项目
【发布时间】:2021-10-22 06:18:01
【问题描述】:

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

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()

【问题讨论】:

    标签: python tkinter listbox


    【解决方案1】:

    您可以将nlist 设为元组(字母、数字)列表,如下所示:

    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()的官方文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多