Skip to content Skip to sidebar Skip to footer

Tkinter Listbox How To Tell If An Item Is Selected

I am trying to create a simple GUI that allows the user to press a button, which will delete an entry from a shown Listbox. However, the console throws an error if no entry is sele

Solution 1:

Instead of returning None like you're checking for, it returns an empty string, "". Check for that as follows:

if selection:
    self.recipe_list.delete(selection)
else:
    print("Nothing to delete!")

Solution 2:

According with

Tkinter reference: a GUI for Python

Methods on listbox objects include:

.curselection()

Returns a tuple containing the line numbers of the selected element or elements, counting from 0.

If nothing is selected, returns an empty tuple.

So you can do somenthing like this

ifself.MyListbox.curselection():
        index = self.MyListbox.curselection()[0]

Solution 3:

ifnot self.lstb.curselection() is ():

Post a Comment for "Tkinter Listbox How To Tell If An Item Is Selected"