我正在尝试将 Kivy 用作我的 python 应用程序的 GUI,它需要从文件系统中读取文件。但是,在某些情况下,当我尝试为文本字段的文本设置读取路径时,kivy 文件选择器读取了错误的路径或没有导致 IndexError。我使用默认示例来读取从http://kivy.org/docs/api-kivy.uix.filechooser.html学习的文件 我的应用程序的相关部分在此函数中,其中添加了异常处理作为非处理这个的好方法:)
def load(self, path, filename):
'''
this will load the file and dismiss the dialog
'''
print "Loading file..."
print "filename:",filename
print "path:",path
try:
self.selected_file = filename[0]
self.file_text_input.text = self.selected_file
self.dismiss_popup()
except IndexError as ie:
print "Something made a boo-boo...try again"+str(ie)
self.dismiss_popup()
self.show_popup("ERROR","Somehow I couldn't load the file:\nCheck the permissions or move it to other place")
self.show_popup() 只是一个辅助函数,它显示一个带有设置函数参数的弹出窗口。
基本错误是 filename[0] 会抛出一个 IndexError 因为它没有读取正确的路径。我正在使用带有 python2.7 的 Linux,有时当我在我的主文件夹中选择一个文件时,文件名变量不存储任何内容,而路径变量则神秘地存储一个随机文件夹,例如 /media、/opt 等。
有人遇到过这个问题吗?