簡體   English   中英

Python Tkinter-刪除選定的列表框項目

[英]Python tkinter- deleting selected listbox item

我需要能夠突出顯示給定列表框中的一個或多個項目並將其刪除。 我在這里查看了其他人的問題,但是由於某些原因,當我嘗試刪除項目時,什么也沒有發生。

以下是我正在使用的代碼。 任何人都可以看看並幫助我嗎?

import tkinter

class Remove_Button_Widget():

    def __init__(self):
        self.Remove_Button = tkinter.Button(master, text= "Remove", height = 2, width = 6, command =lambda :remove_button().remove_functionality(Robot_Files_Found_Widgets().ROBOT_FILE_LIST))
        self.Remove_Button.place(x=362,y=350)

class Robot_Files_Found_Widgets():


    def __init__(self):
        self.Robot_Files_Ran_Frame = tkinter.Frame(master)
        self.Robot_Files_Ran_Frame.place(bordermode=tkinter.INSIDE, height=30, width=200, y=250, x=35)

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

        self.Scroll_Bar_x = tkinter.Scrollbar(self.ROBOT_FILE_LIST, orient=tkinter.HORIZONTAL)
        self.Scroll_Bar_x.config(command=self.ROBOT_FILE_LIST.xview)
        self.Scroll_Bar_x.pack(fill=tkinter.X, side=tkinter.BOTTOM)
        self.ROBOT_FILE_LIST.config(xscrollcommand=self.Scroll_Bar_x.set)
        self.Scroll_Bar_y = tkinter.Scrollbar(self.ROBOT_FILE_LIST, orient=tkinter.VERTICAL)
        self.Scroll_Bar_y.config(command=self.ROBOT_FILE_LIST.yview)
        self.Scroll_Bar_y.pack(fill=tkinter.Y, side=tkinter.RIGHT)
        self.ROBOT_FILE_LIST.config(yscrollcommand=self.Scroll_Bar_y.set)
        list = []
        for x in range(0,15):
            list.append(x)
        for y in list:
            self.ROBOT_FILE_LIST.insert(0,y)

class remove_button():

    def remove_functionality(self, ROBOT_FILE_LIST):
        sel = ROBOT_FILE_LIST.curselection()
        # iterate over sel, deleting each item
        for index in sel:
            ROBOT_FILE_LIST.delete(index)

if __name__ == "__main__":


    master = tkinter.Tk()
    master.title("Test Runner")
    master.geometry("750x500")
    master.resizable(width=False, height=False)
    Robot_Files_Found_Widgets()
    Remove_Button_Widget()
    master.mainloop()

按鈕不起作用的主要原因是由於您如何構建lambda。 因為您每次按下按鈕都在調用該類,所以您實際上並沒有在編輯用於構建GUI的該類的第一個實例。 您需要保存對類實例的引用才能使其正常工作。 我可以看到應該避免的另一個問題是調用列表list 不要將變量命名為內置方法。 這會破壞代碼中的內容。

我認為您不應建立太多的課程。 您在此處的所有功能都可以內置到一個類中。 也就是說,在刪除索引選擇時還需要用reversed()來避免跳索引。

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

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

只是為了糾正原始問題而不重新編寫整個代碼,您需要保存對列表框的引用,並將其傳遞給按鈕以獲取變量。

這是一個將代碼與保存的變量在類之間傳遞的示例。

import tkinter


class Remove_Button_Widget():
    def __init__(self, var1):
        variable_to_pass = var1
        self.Remove_Button = tkinter.Button(master, text="Remove", height=2, width=6, command=lambda :remove_button().remove_functionality(variable_to_pass))
        self.Remove_Button.place(x=362,y=350)


class Robot_Files_Found_Widgets():
    def __init__(self):
        self.Robot_Files_Ran_Frame = tkinter.Frame(master)
        self.Robot_Files_Ran_Frame.place(bordermode = tkinter.INSIDE, height=30, width=200, y=250, x=35)
        self.Display_Robot_Files_Frame = tkinter.Frame(master, borderwidth=1, highlightthickness=1,
                                              highlightbackground="black", highlightcolor="black")
        self.Display_Robot_Files_Frame.place(bordermode = tkinter.INSIDE, height=200, width=300, y=285, x=50)
        self.ROBOT_FILE_LIST = tkinter.Listbox(self.Display_Robot_Files_Frame,selectmode = tkinter.MULTIPLE)
        self.ROBOT_FILE_LIST.place(bordermode = tkinter.INSIDE, height=196, width=296)

        self.Scroll_Bar_x = tkinter.Scrollbar(self.ROBOT_FILE_LIST, orient = tkinter.HORIZONTAL)
        self.Scroll_Bar_x.config(command=self.ROBOT_FILE_LIST.xview)
        self.Scroll_Bar_x.pack(fill = tkinter.X, side = tkinter.BOTTOM)
        self.ROBOT_FILE_LIST.config(xscrollcommand=self.Scroll_Bar_x.set)
        self.Scroll_Bar_y = tkinter.Scrollbar(self.ROBOT_FILE_LIST, orient = tkinter.VERTICAL)
        self.Scroll_Bar_y.config(command=self.ROBOT_FILE_LIST.yview)
        self.Scroll_Bar_y.pack(fill = tkinter.Y, side = tkinter.RIGHT)
        self.ROBOT_FILE_LIST.config(yscrollcommand=self.Scroll_Bar_y.set)
        some_list = []
        for x in range(0,15):
            some_list.append(x)
        for y in some_list:
            self.ROBOT_FILE_LIST.insert(0, y)
        Remove_Button_Widget(self.ROBOT_FILE_LIST)


class remove_button():
    def remove_functionality(self, ROBOT_FILE_LIST):
        sel = ROBOT_FILE_LIST.curselection()
        # iterate over sel, deleting each item
        for index in reversed(sel):
            ROBOT_FILE_LIST.delete(index)


if __name__ =="__main__":
    master = tkinter.Tk()
    master.title("Test Runner")
    master.geometry("750x500")
    master.resizable(width=False, height=False)
    Robot_Files_Found_Widgets()
    master.mainloop()

.curselection()方法返回所選項目的索引的元組。 方法.delete()接受問題頂部錯誤中所述的數據,它將不接受元組。

您已將列表框的selectmode設置為MULTIPLE,因此您需要遍歷curselection()返回的元組,並curselection()刪除每個索引。

def remove_functionality(self,ROBOT_FILE_LIST):
    sel = ROBOT_FILE_LIST.curselection()
    # iterate over sel, deleting each item
    for index in sel[::-1]:
        ROBOT_FILE_LIST.delete(index)

暫無
暫無

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

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