问题描述
限时送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:关键词]
发布评论