使用Entry搜索列表框中的多个条目(tkinter)

时间:2014-12-03 20:52:59

标签: python search tkinter listbox tkinter-entry

我想使用搜索功能搜索程序名称列表(chrome,thunderbird,adobe reader等)

我有一个列表框,然后在列表框中添加了一大堆程序名称,我希望搜索功能entry_1搜索所有这些名称,然后突出显示搜索到的名称。

这可能吗?

这是我的代码:

from tkinter import *

    
#definitions
root = Tk()
var_1 = StringVar(root)
label_1 = Label(root, text="Search:")
label_2 = Label(root, text="Subject specific programs below")
label_3 = Label(root, text="Subject:")
entry_1 = Entry(root) #this should search through the strings listed under listbox_2 configs
button_1 = Button(root, text="Install")
scrollbar_1 = Scrollbar(root)
listbox_2 = Listbox(root, yscrollcommand=scrollbar_1.set)
optionmenu_1 = OptionMenu(root, var_1, "Computing", "Engineering", "Physics")
string_1 = StringVar(root, name="Google Chrome")
string_2 = StringVar(root, name="Thunderbird")
string_3 = StringVar(root, name="Adobe Reader X")
string_4 = StringVar(root, name="WinRAR")
string_5 = StringVar(root, name="OpenOffice")
string_6 = StringVar(root, name="Program 1")
string_7 = StringVar(root, name="Program 2")
string_8 = StringVar(root, name="Program 3")
string_9 = StringVar(root, name="Program 4")
string_10 = StringVar(root, name="Program 5")
string_11 = StringVar(root, name="Program 6")

#configuration
root.title("Network Installation")
listbox_2.insert(1, string_1)
listbox_2.insert(2, string_2)
listbox_2.insert(3, string_3)
listbox_2.insert(4, string_4)
listbox_2.insert(5, string_5)
listbox_2.insert(6, string_6)
listbox_2.insert(7, string_7)
listbox_2.insert(8, string_8)
listbox_2.insert(9, string_9)
listbox_2.insert(10, string_10)
listbox_2.insert(11, string_11)
optionmenu_1.config(width=15)
scrollbar_1.config(command=listbox_2.yview)

#grid additions
label_1.grid(row=0, column=5)
label_2.grid(columnspan=6, row=1, column=0, sticky=E)
entry_1.grid(row=0, column=6)
button_1.grid(columnspan=2, row=7, column=5)
listbox_2.grid(rowspan=6, columnspan=6, row=2, column=0)
scrollbar_1.grid(rowspan=6, row=2, column=4, sticky=N+S)
optionmenu_1.grid(columnspan=3, row=0, column=1)
label_3.grid(row=0, column=0)


root.mainloop()

1 个答案:

答案 0 :(得分:0)

我知道这是很久以前的问题,但是这里是如何使用Tkinter中的搜索输入框突出显示列表框项目。

说明:

  1. all_listbox_items是最重要的部分。当程序首次填充列表框时,捕获这些初始值并使用搜索条目框作为查询这些初始条目的方式非常重要。
  2. textvariable添加到entry_1并将其命名为search_var
  3. search_var进行了跟踪,因此,如果entry_1中的值发生变化,我们就可以触发名为highlight_searched的函数。
  4. 函数highlight_searched基本上enumerates所有列表框项目,并检查entry_1中的搜索文本是否在列表框中的任何项目中。如果这是真的,你可以使用selection_set(i)(其中我是索引的简称)来选择每个项目。
  5. 我对使用枚举并不是特别满意。这很简单。肯定有一种更好的方法来返回for循环中项的索引。我很想听听我的枚举建议的更好解决方案。

    代码:

    from tkinter import *
    
    #definitions
    root = Tk()
    var_1 = StringVar(root)
    label_1 = Label(root, text="Search:")
    label_2 = Label(root, text="Subject specific programs below")
    label_3 = Label(root, text="Subject:")
    
    def highlight_searched(*args):
      search = search_var.get()
      for i,item in enumerate(all_listbox_items):
        if search.lower() in item.lower():
          listbox_2.selection_set(i)
        else:
          listbox_2.selection_clear(i)
      if search == '':
        listbox_2.selection_clear(0, END)
    
    search_var = StringVar()
    search_var.trace('w', highlight_searched)
    entry_1 = Entry(root, textvariable=search_var) #this should search through the strings listed under listbox_2 configs
    
    button_1 = Button(root, text="Install")
    scrollbar_1 = Scrollbar(root)
    listbox_2 = Listbox(root, yscrollcommand=scrollbar_1.set)
    optionmenu_1 = OptionMenu(root, var_1, "Computing", "Engineering", "Physics")
    string_1 = StringVar(root, name="Google Chrome")
    string_2 = StringVar(root, name="Thunderbird")
    string_3 = StringVar(root, name="Adobe Reader X")
    string_4 = StringVar(root, name="WinRAR")
    string_5 = StringVar(root, name="OpenOffice")
    string_6 = StringVar(root, name="Program 1")
    string_7 = StringVar(root, name="Program 2")
    string_8 = StringVar(root, name="Program 3")
    string_9 = StringVar(root, name="Program 4")
    string_10 = StringVar(root, name="Program 5")
    string_11 = StringVar(root, name="Program 6")
    
    #configuration
    root.title("Network Installation")
    listbox_2.insert(1, string_1)
    listbox_2.insert(2, string_2)
    listbox_2.insert(3, string_3)
    listbox_2.insert(4, string_4)
    listbox_2.insert(5, string_5)
    listbox_2.insert(6, string_6)
    listbox_2.insert(7, string_7)
    listbox_2.insert(8, string_8)
    listbox_2.insert(9, string_9)
    listbox_2.insert(10, string_10)
    listbox_2.insert(11, string_11)
    optionmenu_1.config(width=15)
    scrollbar_1.config(command=listbox_2.yview)
    
    #grid additions
    label_1.grid(row=0, column=5)
    label_2.grid(columnspan=6, row=1, column=0, sticky=E)
    entry_1.grid(row=0, column=6)
    button_1.grid(columnspan=2, row=7, column=5)
    listbox_2.grid(rowspan=6, columnspan=6, row=2, column=0)
    scrollbar_1.grid(rowspan=6, row=2, column=4, sticky=N+S)
    optionmenu_1.grid(columnspan=3, row=0, column=1)
    label_3.grid(row=0, column=0)
    
    all_listbox_items = listbox_2.get(0, END)
    
    root.mainloop()
    

    任何显示此问题的人可能正在寻找一个过滤列表框的搜索框(而不是像这个问题那样突出显示它们),只显示与搜索相关的相关项目,您可以使用:

    import tkinter as tk
    
    root = tk.Tk()
    def update_listbox(*args):
      search_term = search_var.get()
      listbox.delete(0, tk.END)
      for item in all_items:
        if search_term.lower() in item.lower():
          listbox.insert(tk.END, item)
    
    search_var = tk.StringVar()
    search_var.trace('w', update_listbox)
    searchbox = tk.Entry(root, textvariable=search_var)
    searchbox.pack(fill=tk.X, expand=False)
    
    listbox = tk.Listbox(root)
    for i in ['Adam', 'Lucy', 'Barry', 'Bob']:
      listbox.insert(tk.END, i)
    listbox.pack()
    all_items = listbox.get(0, tk.END)
    root.mainloop()