Tkinter搜索/过滤条 - csv文件

时间:2016-05-16 10:59:49

标签: python csv tkinter listbox

引用http://code.activestate.com/recipes/578860-setting-up-a-listbox-filter-in-tkinterpython-27/有没有办法做到这一点,但有两个或更多的列表框?例如:名字和姓氏列表框。我试图这样做,但是当我希望它们被链接时,它会单独搜索两列。

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,你就不会这样:

from Tkinter import *

# First create application class


class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)

        self.lbox_list = [('Adam', 'Mitric' ),
                           ('Lucy', 'Devic'  ), 
                           ('Bob' , 'Freamen'), 
                           ('Amanda', 'Ling' ), 
                           ('Susan', 'Cascov')]

        self.pack()
        self.create_widgets()

    # Create main GUI window
    def create_widgets(self):
        self.search_var = StringVar()
        self.search_var.trace("w", lambda name, index, mode: self.update_list())
        self.entry = Entry(self, textvariable=self.search_var, width=13)
        self.lbox1 = Listbox(self, width=20, height=15)
        self.lbox2 = Listbox(self, width=20, height=15)         # Second List Box. Maybe you can use treeview ? 

        self.entry.grid(row=0, column=0, padx=10, pady=3)
        self.lbox1.grid(row=1, column=0, padx=10, pady=5)
        self.lbox2.grid(row=1, column=1, padx=10, pady=5)

        # Function for updating the list/doing the search.
        # It needs to be called here to populate the listbox.
        self.update_list()

    def update_list(self):
        search_term = self.search_var.get()

        # Just a generic list to populate the listbox

        self.lbox1.delete(0, END)
        self.lbox2.delete(0, END)       # Deletng from second listbox

        passed = []                     # Need this to check for colisions

        for item in self.lbox_list:
            if search_term.lower() in item[0].lower():
                self.lbox1.insert(END, item[0])
                self.lbox2.insert(END, item[1])
                passed.append(item)

        for item in self.lbox_list:
            if search_term.lower() in item[1].lower() and item not in passed:
                self.lbox1.insert(END, item[0])
                self.lbox2.insert(END, item[1])


root = Tk()
root.title('Filter Listbox Test')
app = Application(master=root)
app.mainloop()