我有一个Kivy APP,可以对数据文件中的单词进行计数。首先,我使用FileChooser选择一个文件(如果未选择该文件,则一切正常):
FileChooserListView:
canvas.before:
Color:
rgb: .4, .5, .5
on_selection: root.select(*args)
然后按此:
并得到此错误:
File "C:\GUI Projects\wordcount\wordcount.kv", line 53, in <module>
on_selection: root.select(*args)
File "wordcount.py", line 15, in select
filepath = args[1][0]
IndexError: list index out of range
表示此块,第四行:
class DocxPptxWindow(Screen):
def select(self, *args):
filepath = args[1][0]
text = txt.process(filepath)
text = text.decode("utf8")
text = text.replace("\n", " ") #change tags to spaces
text = text.replace("\t", " ")
text = text.replace(" ", " ") #delete continous doublespaces
text = text.replace(" ", " ")
text = text.replace(" ", " ")
text = text.split() #convert string into list of words
word_list = [w for w in text if w.isalnum()] #deleting non-alnum
num_w = len(word_list)
try: self.label.text = str(num_w)
except: pass
显然,我需要以某种方式“关闭”它,但是我没有在Kivi Tutorial中找到它,并且由于缺乏经验而事先道歉。
或者我需要确定将在“选择”下执行的条件,类似于this。但是该解决方案是针对IndexError: tuple index out of range
错误,而不是IndexError: list index out of range
。
非常感谢您的帮助。
通过添加解决if args[1]:
后def select(self, *args):