Selectmode multiple与动态搜索栏Tkinter一起使用

时间:2017-11-30 11:50:41

标签: python tkinter

我正在尝试使用动态搜索功能,用户也可以在列表中选择多个项目。如果我们考虑这个example(我添加了这部分,selectmode=MULTIPLE):

from Tkinter import *

# First create application class   
class Application(Frame):

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

    self.pack()
    self.create_widgets()

# Create main GUI window
def create_widgets(self):
    self.search_var = StringVar()
    self.search_var.trace("w", self.update_list)
    self.entry = Entry(self, textvariable=self.search_var, width=13)
    self.lbox = Listbox(self,selectmode=MULTIPLE, width=45, height=15)

    self.entry.grid(row=0, column=0, padx=10, pady=3)
    self.lbox.grid(row=1, column=0, padx=10, pady=3)
    self.btn = ttk.Button(self, text="Select", command=self.Select)
    self.btn.grid(column=1, row=1) 

    # 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, *args):
    search_term = self.search_var.get()

    # Just a generic list to populate the listbox
    lbox_list = ['Adam', 'Lucy', 'Barry', 'Bob',
                 'James', 'Frank', 'Susan', 'Amanda', 'Christie']

    self.lbox.delete(0, END)

    for item in lbox_list:
            if search_term.lower() in item.lower():
                self.lbox.insert(END, item)

def Select(self):

    reslist = list()
    selecion = self.lbox.curselection()

    for i in selecion:
        entered = self.lbox.get(i)
        reslist.append(entered)

    print reslist 

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

搜索功能完全正常,但是,一旦完成搜索并选择了某个项目,由于在lbox.delete中使用update_list功能,因此不会保存选择。有没有办法在使用搜索功能时保留每个项目?

1 个答案:

答案 0 :(得分:0)

这是我想出的。我不知道您是否需要它,但我还是会张贴它。 我基本上已经添加了代码,以便在Entry小部件中的更改刷新列表框后将其设置为用户选择列表中的值时将其设置为选定的值,并且如果用户取消选择而不考虑列表框中当前的内容,则将其从用户选择中删除。 / p>

from tkinter import *

sel=list()

# First create application class
class Application(Frame):

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

        self.pack()
        self.create_widgets()

    def CurSelet(self,evt):
        global sel
        temp=list()
        for i in self.lbox.curselection():
            temp.append(self.lbox.get(i))

        allitems=list()
        for i in range(self.lbox.size()):
            allitems.append(self.lbox.get(i))

        for i in sel:
            if i in allitems:
                if i not in temp:
                    sel.remove(i)

        for x in self.lbox.curselection():
            if self.lbox.get(x) not in sel:
                sel.append(self.lbox.get(x))

    def select(self):
        global sel
        s=', '.join(map(str,sel))
        self.cursel.set('Current Selection: '+s)

    # 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.lbox = Listbox(self, selectmode=MULTIPLE,width=45, height=15)
        self.lbox.bind('<<ListboxSelect>>',self.CurSelet)

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

        self.btn=Button(self,text='Okay', command=self.select, width=20)
        self.btn.grid(row=2,column=0, padx=10, pady=3)

        self.cursel=StringVar()
        self.lb1=Label(self,textvariable=self.cursel)
        self.lb1.grid(row=3,column=0,padx=10,pady=3)

        # 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):
        global sel
        global l
        search_term = self.search_var.get()

        # Just a generic list to populate the listbox
        lbox_list = ['Adam', 'Lucy', 'Barry', 'Bob',
                     'James', 'Frank', 'Susan', 'Amanda', 'Christie']

        self.lbox.delete(0, END)

        for item in lbox_list:
                if search_term.lower() in item.lower():
                    self.lbox.insert(END, item)

        allitems=list()
        for i in range(self.lbox.size()):
            allitems.append(self.lbox.get(i))

        for i in sel:
            if i in allitems:
                self.lbox.select_set(self.lbox.get(0, "end").index(i))

root = Tk()
root.title('Filter Listbox Test')
Label(root, text='Search enabled').pack()
app = Application(master=root)
print('Starting mainloop()')
app.mainloop()