【发布时间】:2020-02-13 09:37:41
【问题描述】:
我有一个 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。
非常感谢任何帮助。
【问题讨论】:
标签: python indexing kivy filechooser index-error