Tkinter双击删除ListBoxvalues

时间:2019-12-19 17:35:14

标签: tkinter listbox

我想通过双击从ListBox中删除一个值。
我不明白如何通过Tkinter事件获取import tkinter as tk def addValuesListBox(listbox): for i in range(10): listbox.insert(tk.END, i) def removeValue(event): #here i'd like to remove the value to the corresponding listbox value print("remove value") if __name__ == '__main__': window = tk.Tk() listbox = tk.Listbox(window) addValuesListBox(listbox) listbox.bind( "<Double-Button-1>" , removeValue ) listbox.pack() window.mainloop() 项目的值

这是我到目前为止所做的:

{{1}}

1 个答案:

答案 0 :(得分:2)

这就是你想要的。

import tkinter as tk

def addValuesListBox(listbox):
    for i in range(10):
        listbox.insert(tk.END, i)

def removeValue(event):
    selection = listbox.curselection()
    print(selection)
    listbox.delete(selection)
    print("remove value")

if __name__ == '__main__':
    window = tk.Tk()
    listbox = tk.Listbox(window)
    addValuesListBox(listbox)
    listbox.bind( "<Double-Button-1>" , removeValue )
    listbox.pack()
    window.mainloop()