我试图使用Kivy作为我的python应用程序的GUI,它需要从文件系统读取文件。 但是,在某些情况下,当我尝试为文本字段的文本设置读取路径时,kivy filechooser读取错误的路径或导致IndexError失效。 我使用默认例如读取文件从http://kivy.org/docs/api-kivy.uix.filechooser.html 我的应用程序的相关部分在此功能,在异常处理添加为不是一个好方法来处理这个教训:)python kivy filechooser读取错误路径
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")
自我。 show_popup()只是一个辅助函数,它显示了一个带有set函数params的弹出窗口。
基本错误是文件名[0]会抛出一个IndexError,因为它没有读取正确的路径。 我在python2.7中使用Linux,当我在我的home文件夹中选择一个文件时,文件名变量什么都不存储,而path变量存储一个随机文件夹,例如/ media,/ opt等等。
有没有人遇到过这个问题?