I created a FileChooser (below) in Kivy that displays directories and not files. But, how do I create a FileChooser in Kivy that will display only files and not directories.
My KV code:
FileChooserListView:
id: MTDcontainer
size_hint_y: .87
size_hint_x: .95
halign: "left"
pos_hint: {"center_x": .5, "center_y": .60}
on_selection: app.MTDirWasChosen_callback(args)
path: "."
filters: [lambda folder, filename: not filename.endswith('')] #display dirs only
dirselect: True
...
You can do that using some attributes of the FileChooser
along with a custom filter. First, change your path
attribute to rootpath
instead. That eliminates the ..
entry that normally is displayed. Then you can add a method that filters out directories, but you must also add the filter_dirs: True
in order for the filter to be applied to folders. Here is a modified version of your kv
that does that:
FileChooserListView:
id: MTDcontainer
size_hint_y: .87
size_hint_x: .95
halign: "left"
pos_hint: {"center_x": .5, "center_y": .60}
on_selection: app.MTDirWasChosen_callback(args)
rootpath: "."
filters: [app.file_filter]
filter_dirs: True
And the file_filter()
method in the App
is just:
def file_filter(self, folder, file):
return not os.path.isdir(file)