Selectmode 多个连同动态搜索栏 Tkinter

编程入门 行业动态 更新时间:2024-10-17 12:29:57
本文介绍了Selectmode 多个连同动态搜索栏 Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试执行动态搜索功能,用户还可以在其中选择列表中的多个项目.如果我们考虑这个示例(我添加了这部分,selectmode=MULTIPLE):

I am trying to do a dynamic search function, where the user also can select multiple items in a list. If we consider this example (I added this part, 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 中使用.有没有办法在使用搜索功能时保持每个项目都被选中?

The search function works perfectly fine, however, once a search has been done and an item has been selected, the selections is not saved since the lbox.delete function is used in update_list. Is there a way to keep each item selected while using the search function?

推荐答案

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

This is what I came up with. I don't know if you need it anymore but I'll post it anyway. I've basically added code to set values as selected if they are in user selection list after the listbox is refreshed on a change in Entry widget, and to remove it from user selection if user deselects irrespective of what is currently in the listbox.

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

这篇关于Selectmode 多个连同动态搜索栏 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:08:22,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   动态   Selectmode   Tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!