【问题标题】:FileChooser crashes Kivy APP when 'back' (to the higher level of dir.) is selected in FileChooserListView: how to fix?在 FileChooserListView 中选择“返回”(到更高级别的目录)时,FileChooser 使 Kivy APP 崩溃:如何修复?
【发布时间】: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


    【解决方案1】:

    通过添加解决 if args[1]:def select(self, *args):

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多