Python Tkinter-删除列表框项目和相应的列表项目 [英] Python tkinter- Delete listbox item and corresponding list item

查看:176
本文介绍了Python Tkinter-删除列表框项目和相应的列表项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经能够从一个列表框中删除一个或多个列表框项目,但是我也希望能够从实际列表中删除这些确切的项目.如果您运行该代码,那么我所提供的代码将使您看到相反的列表项正在被删除.例如,如果我突出显示"14"并将其删除,则从列表中删除的项目为"0",因为我们的范围是(0,15).请看一看,感谢您提供任何反馈意见.

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

推荐答案

由于索引的工作方式以及尝试从匹配项的列表中仅删除索引时遇到的问题,我认为最好获取列表框中所选项目的显示值,然后从列表中删除这些项目,然后重新构建列表框.这样可以使列表和收件箱之间的索引不匹配.

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.

首先将some_list更改为self.some_list,以使其成为类属性,稍后我们可以从类方法中进行访问.

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

然后将您的remove_functionality(self)方法更改为以下内容:

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)

这篇关于Python Tkinter-删除列表框项目和相应的列表项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆