[英]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.
在我用作示例的代码中,我对其进行了简化,以便第一个列表框具有从 a 到 j 的字母列表,然后重复此列表,但这些字母旁边有一个数字。 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.
现在我希望能够键入“a”,它会显示列表 1 中以“a”开头的项目,然后在第二个列表框中显示与列表框 1 中的项目相对应的数字。例如,a应该有1对应,b应该有2,依此类推。 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.
我已经想出如何让第一个列表框显示所有以我键入的字母开头的项目,但我不知道如何让第二个列表框显示 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()
You can make nlist
a list of tuple (alpha, number) as below:您可以使
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)
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:或者甚至删除
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.