You can use the following code -to delete the elements from a Listbox when a button is clicked.
The simplest method you can use to delete items in the Listbox called as ‘lb’ is to delete items from the first to the last element of the list with the delete method.
Python listbox clear
1 2 3 |
lb.delete(0,'end') |
Example : To clear elements from listbox by clicking a button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# -*- coding: utf-8 -*- import tkinter as tk #clear listbox def clearListbox(): lb.delete(0,'end') root = tk.Tk() root.title("Code4Example.com") # Add a title tk.Button(root,text="Clear Listbox", command=clearListbox).grid(row = 0, column = 0) var2 = tk.StringVar() var2.set(('Apple','Banana','Pear', 'Peach')) lb = tk.Listbox(root, listvariable=var2) lb.grid(row = 0, column = 1) root.mainloop() |