我在 Kivy 中創建了一個 FileChooser(如下),它顯示目錄而不是檔案。但是,如何在 Kivy 中創建一個僅顯示檔案而不顯示目錄的 FileChooser 。
我的 KV 代碼:
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
...
uj5u.com熱心網友回復:
您可以使用 的某些屬性和FileChooser自定義過濾器來做到這一點。首先,將您的path屬性更改為rootpath。這消除了..通常顯示的條目。然后,您可以添加過濾掉目錄的方法,但您還必須添加filter_dirs: True過濾器才能將過濾器應用于檔案夾。這是您的修改版本kv:
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
中的file_filter()方法App只是:
def file_filter(self, folder, file):
return not os.path.isdir(file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411292.html
標籤:
