简体   繁体   中英

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

I have two list boxes, one has words, and the other will have numbers. In the code I am using as an example, I simplified it so that the first list box has a list of letters going from a to j and then repeats this list, but with a number next to those letters. The second list box has just numbers. Now I want to be able to type 'a' and it show the items in list 1 that start with 'a', and then in the second list box show the numbers that correspond to the items in list box 1. For example, a should have 1 correspond to it, b should have 2, and so on. I have already figured out how to make the first list box show all the items that start with the letter I type, but I have no idea how to make the second list box show the numbers that go with those items.

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

You can make nlist a list of tuple (alpha, number) as below:

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)

Note that I prefer using list comprehension instead:

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

Or even remove the creation of nlist and just use a for loop:

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)

Refer to the official document about zip() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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