pythontkinterlistbox

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 selected, so how would I determine if the user has selected an entry. Here's my code:

selection = self.recipe_list.curselection()
    if not selection is None:
        self.recipe_list.delete(selection)
    else:
        print("Nothing to delete!")

Solution

  • 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!")