当从fileChooser.selection读取时,我得到了一个到choosen文件的几乎正确的路径,但是路径中还有一个不存在的文件夹。似乎文件类型是在“MY\u GUI\u KIVY”之后添加的。我错过了什么
代码是GUI的一部分,应该有助于管理项目。要加载相关文件,我需要获取该文件的路径。我试着自己找出问题,在网上搜索找到类似的问题并最终采用他们给出的解决方案后,我找不到一个可行的解决方案
def loadFile(self, fileType):
self.getPath(fileType)
# code shortened for better readability
return
def getPath(self, fileType):
# create popup layout
content = BoxLayout(orientation='vertical', spacing=5)
popup_width = 500
self.popup = popup = Popup(title='Open ' + str(fileType), content=content, size_hint=(None, 0.9), width=popup_width)
# create the filechooser
initDir = 'projects'
if not fileType == 'project':
initDir += '\\' + fileType
initDir = initDir + '\\'
textinput = FileChooserListView(path=initDir, size_hint=(1, 1), dirselect=False, filters=['*.' + fileType])
self.textinput = textinput
# construct the content
content.add_widget(textinput)
# 2 buttons are created for accept or cancel the current value
btnlayout = BoxLayout(size_hint_y=None, height='50dp', spacing='5dp')
btn = Button(text='Ok')
btn.bind(on_release=partial(self.loadSelectedFile, fileType, textinput, popup))
btnlayout.add_widget(btn)
btn = Button(text='Cancel')
btn.bind(on_release=popup.dismiss)
btnlayout.add_widget(btn)
content.add_widget(btnlayout)
# all done, open the popup !
popup.open()
return
def loadSelectedFile(self, fileType, fileChooser, popup, *args): # *args ist unnötig, aber da die partial den Button auch noch zwingend mitliefert...
log.debug(str(fileChooser.selection))
# code shortened for better readability
return True
如果我将fileType设置为“project”,则输出如下: 'C:\GUI\MY\u KIVY\u GUI\projects\projects\test.project'
预期的路径应该是:“C:\GUI\MY\u KIVY\u GUI\projects\test.project”
如果fileType设置为“subproject”,则输出如下所示: 'C:\GUI\MY\u KIVY\u GUI\projects\subproject\projects\subproject\test.subproject'
正确的路径应该是: 'C:\GUI\MY\u KIVY\u GUI\projects\subproject\test.subproject'
所以看起来initDir是在“MY\u KIVY\u GUI”之后添加的,但我不明白为什么会发生这种情况
我看到了和你描述的一样的行为。我想您可能在
FileChooser
代码中发现了一个bug。我认为你的代码应该像写的那样工作。但是,解决方法是添加以下行:在创建
FileChooserListView
之前。这种方法使用绝对路径而不是相对路径,并且似乎有效相关问题 更多 >
编程相关推荐