Search code examples
pythonuser-interfacepython-3.xkivyfilechooser

Kivy FileChooser: List directories only


How to list only directories in Kivy FileChooser? I've read about callback filters, but didn't found any examples.

My Kv code:

<Saveto>:
    select: select
    chooser: chooser
    orientation: 'vertical'
    FileChooserIconView:
        id: chooser
        size_hint_y: 0.9
        rootpath: home
        dirselect: True
        filters: ['How to list folders only?']
    Button:
        ...select button...

Solution

  • Here's an example:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.lang import Builder
    
    from os.path import join, isdir
    
    Builder.load_string("""
    <MyWidget>:
        FileChooserListView:
            filters: [root.is_dir]
    """)
    
    class MyWidget(BoxLayout):
        def is_dir(self, directory, filename):
            return isdir(join(directory, filename))
    
    class MyApp(App):
        def build(self):
            return MyWidget()
    
    if __name__ == '__main__':
        MyApp().run()
    

    Note that the property is called filters, not filter, because it's a list, for example, a list of callbacks.