Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Xóa toàn bộ ListBox Tkinter

Listbox widget tạo danh sách có scroll. Xóa tất cả item: delete(0, END). Xóa 1 item: curselection() lấy index → delete(index).

Ví dụ

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x250")

lb = Listbox(win, width=100, height=5, font=('TkMenuFont', 20))
lb.pack()

def delete():
    lb.delete(0, END)
    Label(win, text="Nothing Found Here!", font=('TkheadingFont', 20)).pack()

lb.insert("end", "item1", "item2", "item3", "item4", "item5")
ttk.Button(win, text="Delete", command=delete).pack()
win.mainloop()

Kết quả

Listbox 5 items. Click "Delete" → Listbox trống, Label "Nothing Found Here!" xuất hiện.

Xóa toàn bộ ListBox Tkinter Xóa toàn bộ ListBox Tkinter

Lưu ý: END là hằng số tkinter.END = "end". delete(0, "end") cũng được.