如何清空 Tkinter ListBox?
为了通过可滚动小部件创建一个项目列表,Tkinter 提供了 Listbox 小部件。有了该 Listbox 小部件,我们可以创建一个包含称为“列表项目”的项目的列表。根据配置,用户可以从列表中选择一个或多个项目。
如果想要清除 Listbox 小部件中的项目,我们可以使用 **delete(0, END)** 方法。除了删除 Listbox 中的所有项目,我们还可以通过从 Listbox 中选择一个项目(即,使用 **currselection()** 方法选择一个项目并使用 **delete()** 函数删除它)来删除单个项目。
示例
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x250")
# Create a Listbox widget
lb=Listbox(win, width=100, height=5, font=('TkMenuFont, 20'))
lb.pack()
# Once the list item is deleted, we can insert a new item in the listbox
def delete():
lb.delete(0,END)
Label(win, text="Nothing Found Here!",
font=('TkheadingFont, 20')).pack()
# Add items in the Listbox
lb.insert("end","item1","item2","item3","item4","item5")
# Add a Button to Edit and Delete the Listbox Item
ttk.Button(win, text="Delete", command=delete).pack()
win.mainloop()输出
如果运行上述代码,它将在列表框中显示一个项目列表和一个按钮以清除 Listbox。

现在,单击“Delete”按钮以清除 Listbox 小部件。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP