How to check if a Listbox in empty in tkinter?

Viewed 1975

I am using Python Tkinter and I need to know if a listbox is empty or not, but I dont´t the syntax to do that. Please help me!

2 Answers

The listbox has a size method that returns a count of the number of items in the listbox:

size = the_listbox.size()

The string "end" is a special lisbox index that refers to the position after the last item in the listbox. Another solution is to get the index of "end". If it's zero, the listbox is empty.

end_index = the_listbox.index("end")
if end_index == 0:
    print("the listbox is empty")

Simply check if the first line is empty:

if not listbox.get(0):
  print('Empty')
else:
  print('Not empty')

or if other lines may be non empty, check the whole content:

if not listbox.get(0,END):
  print('Empty')
else:
  print('Not empty')
Related