为什么 filechooser.selection 添加一个额外的航路点?
Why does filechooser.selection add an additional waypoint?
从 fileChooser.selection 读取时,我得到了一个几乎正确的所选文件路径,但路径中有一个不存在的额外文件夹。 'MY_GUI_KIVY' 之后似乎添加了 fileType。我在这里错过了什么?
该代码是应该有助于管理项目的 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
抱歉,我还不能 post 图片。 Link 应该显示图像。演出
订单结构 - 我不得不模糊一些文件夹,但它们与问题无关
如果我将 fileType 设置为 'project',输出如下所示:
'C:\GUI\MY_KIVY_GUI\projects\projects\test.project'
预期的路径应该是:'C:\GUI\MY_KIVY_GUI\projects\test.project'
如果 fileType 设置为 'subproject',输出如下所示:
'C:\GUI\MY_KIVY_GUI\projects\subproject\projects\subproject\test.subproject'
正确的路径应该是:
'C:\GUI\MY_KIVY_GUI\projects\subproject\test.subproject'
所以似乎在 'MY_KIVY_GUI' 之后添加了 initDir,但我不明白为什么会这样。
我看到了与您描述的相同的行为。我认为您可能发现了 FileChooser
代码中的错误。我认为你的代码应该像写的那样工作。但是,解决方法是添加以下行:
initDir = os.path.abspath(initDir)
就在您创建 FileChooserListView
之前。这使用绝对路径而不是相对路径,并且似乎有效。
从 fileChooser.selection 读取时,我得到了一个几乎正确的所选文件路径,但路径中有一个不存在的额外文件夹。 'MY_GUI_KIVY' 之后似乎添加了 fileType。我在这里错过了什么?
该代码是应该有助于管理项目的 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
抱歉,我还不能 post 图片。 Link 应该显示图像。演出 订单结构 - 我不得不模糊一些文件夹,但它们与问题无关
如果我将 fileType 设置为 'project',输出如下所示: 'C:\GUI\MY_KIVY_GUI\projects\projects\test.project'
预期的路径应该是:'C:\GUI\MY_KIVY_GUI\projects\test.project'
如果 fileType 设置为 'subproject',输出如下所示: 'C:\GUI\MY_KIVY_GUI\projects\subproject\projects\subproject\test.subproject'
正确的路径应该是: 'C:\GUI\MY_KIVY_GUI\projects\subproject\test.subproject'
所以似乎在 'MY_KIVY_GUI' 之后添加了 initDir,但我不明白为什么会这样。
我看到了与您描述的相同的行为。我认为您可能发现了 FileChooser
代码中的错误。我认为你的代码应该像写的那样工作。但是,解决方法是添加以下行:
initDir = os.path.abspath(initDir)
就在您创建 FileChooserListView
之前。这使用绝对路径而不是相对路径,并且似乎有效。