【问题标题】:Selectmode multiple together with dynamic search bar TkinterSelectmode 与动态搜索栏 Tkinter 一起使用
【发布时间】:2018-05-14 08:19:18
【问题描述】:

我正在尝试做一个动态搜索功能,用户还可以在列表中选择多个项目。如果我们考虑这个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 中使用。有没有办法在使用搜索功能时保持选中每个项目?

【问题讨论】:

  • 我会看到this。
  • 感谢您的链接,但是,如果我不使用搜索栏,我了解如何绑定选择。但是,我想搜索 Adam,选择该项目,然后搜索 Lucy,然后调用所选项目(Adam 和 Lucy)。但是,由于列表是动态更新的,并且项目已从列表中删除,因此这是行不通的。
  • 您必须将所选元素保留在列表中,并使用此列表过滤搜索结果。
  • 或者你应该创建一个函数,让你使用"OR" - "Adam OR Lucy" 或"Adam | Lucy" 或简单的"Adam Lucy" 进行搜索。您可以使用正则表达式进行搜索。
  • 我会在选择后将它们附加到一个属性中。

标签: python tkinter


【解决方案1】:

这就是我想出的。我不知道你是否需要它,但无论如何我都会发布它。 我基本上添加了代码来将值设置为选定的值,如果它们在列表框在条目小部件的更改上被刷新后在用户选择列表中,并且如果用户取消选择而不考虑当前在列表框中的内容,则将其从用户选择中删除。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 2021-10-29
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多