python - Treeview 和 Tkinter 删除 treeview 中未选中的项目

嗨,我需要删除 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)

回答1

如果要删除不满足搜索输入的项目,只需在这些项目上调用 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)