Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear Tkinter ListBox Python

How to clear a listbox when a button is clicked to re-populate it? The code below is giving me an error.

code:

self.listNodes.delete(0,END)

error:

NameError: name 'END' is not defined
like image 907
RainbowTeaBag Avatar asked Feb 16 '18 19:02

RainbowTeaBag


People also ask

How to clear a Listbox?

Clear ListBox With the DataSource = null Approach in C# The best solution for this would be to specify the ListBox. DataSource property equal to null to remove the data source and then use the ListBox. Items. Clear() function to clear the previous items in the list box.

How do you deselect a Listbox in Python?

Use the selectmode parameter on the Listbox widget. You can click the selected item again and it will clear the selection.

How many items does a Listbox hold by default?

The default is 20. If you want to allow the user to scroll the listbox horizontally, you can link your listbox widget to a horizontal scrollbar.

What is a tkinter Listbox?

Introduction to the Tkinter Listbox A Listbox allows you to browse through the items and select one or multiple items at once. To create a listbox, you use the tk.Listbox class like this: listbox = tk.Listbox(container, listvariable, height) In this syntax: The container is the parent component of the listbox.


4 Answers

Depending on how you imported tkinter you may have to put end in quotations Try:

  self.listNodes.delete(0,'end')

you can also use:

self.listNodes.delete(0,tk.END)
like image 194
Moller Rodrigues Avatar answered Oct 23 '22 23:10

Moller Rodrigues


Replace:

self.listNodes.delete(0,END)

with:

self.listNodes.delete('0','end')

END is a variable of tkinter module, which suggests either a wildcard(from tkinter import *) import or from tkinter import END was supposed to be used.

like image 32
Nae Avatar answered Oct 23 '22 23:10

Nae


You can use this is you imported tkinter as:

import tkinter as tk

self.listnodes.delete(0, tk.END)

Or you can do:

from tkinter import *

self.listnodes.delete(0, END)
like image 39
Ren Rawbone Avatar answered Oct 24 '22 00:10

Ren Rawbone


Destruction is the best way to delete something.

self.listNode.destroy()

and then add Your data again to your ListBox.

like image 1
Qureshi Taha Avatar answered Oct 23 '22 23:10

Qureshi Taha