簡體   English   中英

Python Tkinter-刪除列表框項目和相應的列表項目

[英]Python tkinter- Delete listbox item and corresponding list item

到目前為止,我已經能夠從一個列表框中刪除一個或多個列表框項目,但是我也希望能夠從實際列表中刪除那些確切的項目。 如果您運行該代碼,那么我所提供的代碼將使您看到相反的列表項正在被刪除。 例如,如果我突出顯示“ 14”並將其刪除,則從列表中刪除的項目為“ 0”,因為我們的范圍是(0,15)。 請看一下,任何反饋表示贊賞。

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

由於索引的工作方式以及您嘗試從與項目匹配的列表中刪除索引時遇到的問題,我認為最好獲取列表框中所選項目的顯示值,然后從中刪除這些項目。列表,然后重建列表框。 這將使列表和收件箱之間的索引不匹配。

首先將some_list更改為self.some_list ,使其成為一個類屬性,稍后我們可以從類方法中訪問它。

然后將您的remove_functionality(self)方法更改為以下內容:

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)

正如Idlehands指出的那樣,上述方法將存在重復項的問題。

作為更好的替代方法,您可以根據刪除項目后列表框中剩余的內容來重建列表。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM