0
votes

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!")
3
what is the error you are getting? - Bryan Oakley
TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number - Andrew Lalis

3 Answers

2
votes

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!")
1
votes

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

if self.MyListbox.curselection():
        index = self.MyListbox.curselection()[0]
0
votes
if not self.lstb.curselection() is ():