Python tkinter- Delete listbox item and correspond

2019-08-25 22:43发布

问题:

So far I have the ability to delete one or multiple listbox items from a listbox but i want to be able to delete those exact items from the actual list as well. If you run the code the code that I provided you will see that it is the opposite ended list item that is getting deleted. For example, if i highlight "14" and remove it, the item removed from the list is "0" since we have a range of (0,15). Please take a look, any feedback is appreciated.

import tkinter as tk


class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Test Runner")
        self.geometry("750x500")
        self.resizable(width=False, height=False)
        self.robot_files_ran_frame = tk.Frame(self)
        self.robot_files_ran_frame.place(bordermode=tk.INSIDE, height=30, width=200, y=250, x=35)

        self.display_robot_files_frame = tk.Frame(self, borderwidth=1, highlightthickness=1,
                                          highlightbackground="black", highlightcolor="black")
        self.display_robot_files_frame.place(bordermode=tk.INSIDE, height=200, width=300, y=285, x=50)
        self.robot_file_list = tk.Listbox(self.display_robot_files_frame,selectmode=tk.MULTIPLE)
        self.robot_file_list.place(bordermode=tk.INSIDE, height=196, width=296)

        self.scroll_bar_x = tk.Scrollbar(self.robot_file_list, orient=tk.HORIZONTAL)
        self.scroll_bar_x.config(command=self.robot_file_list.xview)
        self.scroll_bar_x.pack(fill=tk.X, side=tk.BOTTOM)
        self.robot_file_list.config(xscrollcommand=self.scroll_bar_x.set)
        self.scroll_bar_y = tk.Scrollbar(self.robot_file_list, orient=tk.VERTICAL)
        self.scroll_bar_y.config(command=self.robot_file_list.yview)
        self.scroll_bar_y.pack(fill=tk.Y, side=tk.RIGHT)
        self.robot_file_list.config(yscrollcommand=self.scroll_bar_y.set)
        global some_list
        some_list = []

        for x in range(0,15):
            some_list.append(x)

        for y in some_list:
            self.robot_file_list.insert(0, y)

        self.remove_button = tk.Button(self, text= "Remove", height=2, width=6, command=self.remove_functionality)
        self.remove_button.place(x=362, y=350)

    def remove_functionality(self):
        sel = self.robot_file_list.curselection()
        # added reversed here so index deletion work for multiple selections.
        for index in reversed(sel):
            self.robot_file_list.delete(index)
            del some_list[index]
            print(some_list)

if __name__ == "__main__":
    Example().mainloop()

回答1:

Due to how the indexing works and the issues you will run into if you try to delete just the index from list matching the item I think it would be best to get the display values of the items selected in the listbox, then remove those items from the list and then rebuild the listbox. This will keep out index's matching between the list and inbox.

First change some_list to self.some_list to make it a class attribute we can access from the class method later.

Then change your remove_functionality(self) method to the below:

def remove_functionality(self):
    sel = self.robot_file_list.curselection()
    to_append = []
    for ndex in sel:
        to_append.append(self.robot_file_list.get(ndex))
    for itm in to_append:
        self.some_list.remove(itm)        
    self.robot_file_list.delete(0,'end')
    for y in self.some_list:
        self.robot_file_list.insert(0, y)
    print(self.some_list)

As Idlehands pointed out the above method will have issues with duplicate items.

As a likely better alternative you could just rebuild the list based on what is left in the listbox after you delete items.

def remove_functionality(self):
    sel = self.robot_file_list.curselection()
    for index in reversed(sel):
        self.robot_file_list.delete(index)

    self.some_list = []
    for item in reversed(self.robot_file_list.get(0, "end")):
        self.some_list.append(item)

    print(self.some_list)