2016-05-16 128 views
-1

參考http://code.activestate.com/recipes/578860-setting-up-a-listbox-filter-in-tkinterpython-27/有沒有辦法做到這一點,但有2個或更多的列表框?例如:名字和姓氏列表框。我試圖做到這一點,但它希望它們被鏈接時單獨搜索兩列。Tkinter搜索/篩選條 - csv文件

+0

能否請你解釋一下關於更多的實際問題? –

+0

我想使用輸入標籤在CSV文件中搜索特定結果。這兩列是名字和姓氏。這在問題中提供的鏈接中顯示,但是這僅用於名字,我希望它用於名和姓。當我試圖做到這一點時,它會搜索名字和姓氏,這意味着顯示的結果沒有鏈接(名字和姓氏不匹配)。有沒有辦法搜索名字並顯示結果,以便它們仍然是鏈接的(名字和姓氏是彼此相鄰的) –

回答

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