0
votes

Trying to pop up a message box if no index is selected in a list box instead of performing the action in Python.

 i=Studlistbox.curselection()
if i=='':
    Mbox('Error', 'Nothing has been selected', 0)
else:
    seltext=Studlistbox.get(i)
    seltext=str(seltext)
    seltext=seltext.replace(",",'').replace("(","").replace(")",'').replace("'",'')
    for s in seltext.split(' '):
        if is_number(s):
            seltext=s
....

i get the following error if no item is selected:

File "C:\Python33\lib\tkinter__init__.py", line 2631, in get return self.tk.call(self._w, 'get', first) _tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number

Thanks for the help!

1
so what IS the actual value of i? I'm guessing it's something other than "active, anchor, end, @x,y, or a number" - SpliFF
i is the index in the listbox. if something is selected in the list box everything works fine. - ifonlyirocked
That's what you say i is but the error begs to differ. You need to inspect i yourself and confirm it. - SpliFF
its just trying to grab an empty selection, because nothing is selected. therefor it returns "" when it needs a value. this question got answered here: where its worded better. stackoverflow.com/questions/31757472/… - 314thon

1 Answers

0
votes

According to the error the value of i is something other than "active, anchor, end, @x,y, or a number".

Find out the value and type of i then make sure it is a valid parameter to get. Also even if it looks like a number make sure it's an int and not a str type. if it is a string than convert it to an int first.