Kivy FileChooser:仅列出目录
Kivy FileChooser: List directories only
如何在 Kivy FileChooser 中列出仅个目录?我读过回调过滤器,但没有找到任何示例。
我的Kv代码:
<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...
这是一个例子:
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
,因为它是一个列表,例如回调列表。
如果你想使用没有任何文件的目录,你可以使用:
os.path.dirname(filename)
以下方式(目录仅用于将目录打印为屏幕右侧标签的示例):
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
import os
Builder.load_string("""
<MyWidget>:
FileChooserIconView:
filters: [root.selected]
BoxLayout:
Label:
id: mypath
text: ''
""")
class MyWidget(BoxLayout):
def selected(self, directory, filename):
self.ids.mypath.text = os.path.dirname(filename)
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
希望对您有所帮助
也可以直接在KV中做:
<Saveto>:
select: select
chooser: chooser
orientation: 'vertical'
FileChooserIconView:
id: chooser
size_hint_y: 0.9
rootpath: home
dirselect: True
filters: [lambda folder, filename: not filename.endswith('')]
Button:
...select button...
这有点 hack,但似乎有效并且只能在 kv 文件中完成:
dirselect: True
filters: ['']
FileChooser 中只显示文件夹。
如何在 Kivy FileChooser 中列出仅个目录?我读过回调过滤器,但没有找到任何示例。
我的Kv代码:
<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...
这是一个例子:
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
,因为它是一个列表,例如回调列表。
如果你想使用没有任何文件的目录,你可以使用:
os.path.dirname(filename)
以下方式(目录仅用于将目录打印为屏幕右侧标签的示例):
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
import os
Builder.load_string("""
<MyWidget>:
FileChooserIconView:
filters: [root.selected]
BoxLayout:
Label:
id: mypath
text: ''
""")
class MyWidget(BoxLayout):
def selected(self, directory, filename):
self.ids.mypath.text = os.path.dirname(filename)
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
希望对您有所帮助
也可以直接在KV中做:
<Saveto>:
select: select
chooser: chooser
orientation: 'vertical'
FileChooserIconView:
id: chooser
size_hint_y: 0.9
rootpath: home
dirselect: True
filters: [lambda folder, filename: not filename.endswith('')]
Button:
...select button...
这有点 hack,但似乎有效并且只能在 kv 文件中完成:
dirselect: True
filters: ['']
FileChooser 中只显示文件夹。