2015-12-10 81 views
3

如何列出只有目錄在Kivy FileChooser中?我玩過濾器:Kivy FileChooser:僅列出目錄

filters: ['Directories, not files!'] 

但文件再次列出。我關於回調過濾器的紅色,但沒有找到例子。

千伏代碼:

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

回答

5

下面是一個例子:

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() 

注意,屬性被稱爲filters,不filter,因爲它是一個列表,例如,列表回調。