When reading from fileChooser.selection I get a nearly correct path to the choosen file, but with one additional folder in the path that does not exist. It seems like fileType is added after 'MY_GUI_KIVY'. What am I missing here?
The code is part of a GUI that should help manage projects. To load the related file I need to get the path to that file. After trying to find the problem on my own, searching the net to find similar problems and eventually adopt their given solutions, I could not find a working solution.
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
If I set fileType to 'project', the output is something like that: 'C:\GUI\MY_KIVY_GUI\projects\projects\test.project'
The expected path should have been: 'C:\GUI\MY_KIVY_GUI\projects\test.project'
If fileType is set to 'subproject', the output looks like this: 'C:\GUI\MY_KIVY_GUI\projects\subproject\projects\subproject\test.subproject'
The correct path should have been the following: 'C:\GUI\MY_KIVY_GUI\projects\subproject\test.subproject'
So it seems like initDir is added after 'MY_KIVY_GUI' but I don't get why this happens.
I see the same behavior that you described. I think you may have discovered a bug in the FileChooser
code. I think your code should work as written. However, a work around is to add the line:
initDir = os.path.abspath(initDir)
just before you create the FileChooserListView
. This uses an absolute path instead of a relative path, and seems to work.