繁体   English   中英

我希望能够使用两个搜索栏同时更新两个列表框

[英]I want to be able to update two list boxes at the same time with two searchbars

我希望能够使用两个列表同时更新两个列表框,这两个列表在两个搜索栏中具有相同数量的项目。 第一个列表框有一个包含字母的列表,第二个列表框有直接对应于这些字母的数字,例如 A 与 1 对应,B 与 2 对应,依此类推。 我希望能够在左侧的第一个输入框中键入“a”,并且在第一个列表框中仅显示“a”,在第二个列表框中仅显示“2”,或者在列表框中键入“2”第二个框将显示第二个列表框中具有“2”的所有项目,因此第一个列表框也仅显示与此项目对应的项目。 我已经能够做到一个输入框可以处理但只有两个列表框之一,但我希望两者同时更新。

from tkinter import *  # allows for the use of tkinter GUI

win = Tk()  # creates the tkinter window
win.title("test")  # sets the name of the window
win.geometry("1100x700")  # sets the size of the window
win.resizable(False, False)  # prevents the window from being resized

# these are made up lists for this question
databaselist1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18'
                 , '19', '20']
databaselist2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T']


# -----------------------------------------------------------------------------------------------------------------------
#  this section synchronizes the mouse scrolled for both list boxes
def scroll2(event):
    IDsearchresultslist.yview_scroll(int(-4 * (event.delta / 120)), "units")  # makes it so that when the user scrolls
    # though one list box, all the list boxes scroll as well


def scroll1(event):
    alphabetlist.yview_scroll(int(-4 * (event.delta / 120)), "units")  # makes it so that when the user scrolls
    # though one list box, all the list boxes scroll as well


# -----------------------------------------------------------------------------------------------------------------------
def checkinlist1(e):  # uses the characters typed in their specifically typed order into the ID search entry box in
    # order to display the results that have those characters into the search
    # result box or in other words, an autofill function
    searchtyped = IDsearchEntry.get().replace("'", "`")  # looks at the characters that are being typed into
    # the ID search entry box
    if searchtyped == ' ':  # checks if nothing is typed into the ID search entry box
        listdata1 = databaselist1  # makes it so that the entire list is displayed into the search results box
    else:  # this happens if the ID search entry box has something typed in it
        listdata1 = []
        for item in databaselist1:  # loops though the list
            if searchtyped.lower() in item.lower():
                listdata1.append(item)
    update1(listdata1)  # updates listdata each time the code loops through the checkinlist function


def checkinlist2(e):  # uses the characters typed in their specifically typed order into the nsearch entry box in order
    # to display the results that have those characters into the search result box or in other words
    # , an autofill function

    searchtyped = nsearchentry.get().replace("'", "`")  # looks at the characters that are being typed into the
    # nsearch entry box
    if searchtyped == ' ':  # checks if nothing is typed into the nsearch entry box
        listdata2 = databaselist2  # makes it so that the entire list is displayed into the alphabet listbox
    else:  # this happens if the nsearch entry box has something typed in it
        listdata2 = []
        for item in databaselist2:  # loops though the list
            if searchtyped.lower() in item.lower():
                listdata2.append(item)
    update2(listdata2)  # updates listdata each time the code loops through the checkinlist function


# ----------------------------------------------------------------------------------------------------------------------
# this section sets up the list boxes
alphabetframe = Frame(win)  # creates the frame that will contain the scrollbar for the alphabetframe

IDsearchresultslist = Listbox(win, width=39, font=('Arial', 12, 'bold'))  # creates the

# of the listbox
# ID search list box that will contain the

IDsearchresultslist.place(relx=.6, rely=.15)  # moves the ID search results box to its location

# this section creates the first buttons that will show up when the program is opened
alphabetlist = Listbox(alphabetframe, width=50, font=('Arial', 12, 'bold'))  # creates the search entry box

# of the listbox
alphabetframe.place(relx=.05, rely=.2)  # moves the frame that contains the scrollbar to its location

alphabetlist.pack(pady=15, padx=15)

alphabetlist.bind("<MouseWheel>", scroll2)  # when the mousewheel is used, the scroll2
# function with be activated
IDsearchresultslist.bind("<MouseWheel>", scroll1)  # when the mousewheel is used, the scroll1
# ----------------------------------------------------------------------------------------------------------------------
# this section creates the two entry boxes that will control what the user searches for
IDsearchEntry = Entry(win, width=20, font=('Arial', 15, 'bold'))  # creates the ID search entry box
IDsearchEntry.bind("<KeyRelease>", checkinlist1)  # binds the action of the user clicking on an entry from the list box
# in order for an action to happen
IDsearchEntry.place(relx=.717, rely=.07)  # moves the ID search entry box to its location

nsearchentry = Entry(win, width=17, font=('Arial', 15, 'bold'))  # sets up the nsearch entry box and its parameters
nsearchentry.bind("<KeyRelease>", checkinlist2)  # binds the action of the user clicking on an entry from the list box
# in order for an action to happen
nsearchentry.place(relx=.29, rely=.553)  # moves the nsearchentry box to its location


# ----------------------------------------------------------------------------------------------------------------------
# this section updates the listboxes though their list
def update1(listdata1):  # this function updates the list that shows up inside the IDsearchresultslist as the user types
    IDsearchresultslist.delete(0, END)  # clears the listbox, if END doesn't work use "end"
    for item in listdata1:  # loops though the list
        IDsearchresultslist.insert(END, item)  # moves each individual item from the list into the listbox
        # as the list gets looped


def update2(listdata2):  # this function updates the list that shows up inside the nresultslist as the user types
    alphabetlist.delete(0, END)  # clears the listbox, if END doesn't work use "end"
    for item in listdata2:  # loops though the list
        alphabetlist.insert(END, item)  # moves each individual item from the list into the listbox
        # as the list gets looped


update1(databaselist1)  # makes the databaselist go through the update function
update2(databaselist2)  # makes the databaselist go through the update function

win.mainloop()


  [1]: https://i.stack.imgur.com/OzRT1.png

一种可能的解决方案是 zip 包含数据的列表,将元素添加到列表并将其传递给update(x)() function。

zip() function 会从多个容器中获取相似索引的元素。

而既然要相应更新,那就是go的方法。

def checkinlist1(e):  
    ....
        listdata1 = []
        listdata2=[]
        for item,item2 in zip(databaselist1,databaselist2):  # loops though the list
            if searchtyped.lower() in item.lower():
                listdata1.append(item)
                listdata2.append(item2)
    update2(listdata2)
    update1(listdata1)

....
def checkinlist2(e):  
        ....
        listdata2 = []
        listdata1=[]
        for item,item2 in zip(databaselist2,databaselist1):  # loops though the list
            if searchtyped.lower() in item.lower():
                listdata2.append(item)
                listdata1.append(item2)
    update1(listdata1) 
    update2(listdata2)

参考文献: zip() function in python

暂无
暂无

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

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