嗨,我需要删除Treeview中所有项目,但在窗口中留下搜索项目。请帮忙。此时,搜索项被高亮显示。
####################################################################################################################################################################################################
#ADD LISTBOX
def excel_list():
file ='ddata.xlsx'
listy=Toplevel()
listy.title("EXCEL JOB DATA")
listy.geometry("1000x500")
df = pd.read_excel(file)
#CREATE TREEVIEW FRAME
my_frame = Frame(listy)
my_frame.pack(pady= 20)
tree_scroll = Scrollbar(my_frame)
tree_scroll.pack(side=RIGHT,fill=Y)
my_tree = ttk.Treeview(my_frame,yscrollcommand=tree_scroll.set)
my_tree['column']=list(df.columns)
my_tree['show'] = 'headings'
for column in my_tree['column']:
my_tree.heading(column,text=column)
df_rows = df.to_numpy().tolist()
for row in df_rows:
my_tree.insert('','end',values=row)
my_tree.pack()
tree_scroll.config(command=my_tree.yview)
def search_records():
lookup_record = search_entry.get()
selections = []
print(lookup_record)
for record in my_tree.get_children():
if lookup_record in my_tree.item(record)['values']:
selections.append(record)
my_tree.selection_set(selections)
如果要删除不满足搜索输入的项,只需对这些项调用my_tree.delete(...)
:
def search_records():
lookup_record = search_entry.get()
for record in my_tree.get_children():
if lookup_record not in my_tree.item(record)['values']:
my_tree.delete(record)