python - Validation causing error -


when running following function:

def restoreselection(self, selecteditems):     self.recvlist.selection_clear(0,"end")     items = self.recvlist.get(0,"end")     item in selecteditems:         _i in items:          if item[:6] == _i[:6]:             index = items.index(item)             print index             self.recvlist.selection_set(index) 

i error:

exception in tkinter callback traceback (most recent call last):   file "/usr/lib/python2.7/lib-tk/tkinter.py", line 1437, in __call__     return self.func(*args)   file "/usr/lib/python2.7/lib-tk/tkinter.py", line 498, in callit     func(*args)   file "./pycan1.8.py", line 710, in recvbtn_click     self.restoreselection(selected)   file "./pycan1.8.py", line 443, in restoreselection     index = items.index(item) valueerror: tuple.index(x): x not in tuple 

unfortunately error message isn't terribly clear. please explain error message is? , causing function produce it.

this happened once put nested loop in.

it means value index trying find in items tuple not exist in tuple.

>>> = (1,2) >>> a.index(3) traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: tuple.index(x): x not in tuple 

to avoid can wrap lines trying index try\except block, like:

def restoreselection(self, selecteditems):     self.recvlist.selection_clear(0,"end")     items = self.recvlist.get(0,"end")     item in selecteditems:         _i in items:          if item[:6] == _i[:6]:             try:                 index = items.index(item)             except valueerror:                 index = none #set default value here             print index             self.recvlist.selection_set(index) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -