File Manager¶
A simple manager for selecting directories and files.
Usage¶
path = '/' # path to the directory that will be opened in the file manager
file_manager = MDFileManager(
exit_manager=self.exit_manager, # function called when the user reaches directory tree root
select_path=self.select_path, # function called when selecting a file/directory
)
file_manager.show(path)
Or with previous
mode:
file_manager = MDFileManager(
exit_manager=self.exit_manager,
select_path=self.select_path,
previous=True,
)
Example¶
from kivy.core.window import Window
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.filemanager import MDFileManager
from kivymd.toast import toast
KV = '''
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "MDFileManager"
left_action_items: [['menu', lambda x: None]]
elevation: 10
FloatLayout:
MDRoundFlatIconButton:
text: "Open manager"
icon: "folder"
pos_hint: {'center_x': .5, 'center_y': .6}
on_release: app.file_manager_open()
'''
class Example(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.bind(on_keyboard=self.events)
self.manager_open = False
self.file_manager = MDFileManager(
exit_manager=self.exit_manager,
select_path=self.select_path,
previous=True,
)
def build(self):
return Builder.load_string(KV)
def file_manager_open(self):
self.file_manager.show('/') # output manager to the screen
self.manager_open = True
def select_path(self, path):
'''It will be called when you click on the file name
or the catalog selection button.
:type path: str;
:param path: path to the selected directory or file;
'''
self.exit_manager()
toast(path)
def exit_manager(self, *args):
'''Called when the user reaches the root of the directory tree.'''
self.manager_open = False
self.file_manager.close()
def events(self, instance, keyboard, keycode, text, modifiers):
'''Called when buttons are pressed on the mobile device.'''
if keyboard in (1001, 27):
if self.manager_open:
self.file_manager.back()
return True
Example().run()
API - kivymd.uix.filemanager
¶
-
class
kivymd.uix.filemanager.
MDFileManager
(**kwargs)¶ Float layout class. See module documentation for more information.
-
icon
¶ The icon that will be used on the directory selection button.
icon
is anStringProperty
and defaults to check.
-
icon_folder
¶ The icon that will be used for folder icons when using
previous = True
.icon
is anStringProperty
and defaults to check.
-
exit_manager
¶ Function called when the user reaches directory tree root.
exit_manager
is anObjectProperty
and defaults to lambda x: None.
-
select_path
¶ Function, called when selecting a file/directory.
select_path
is anObjectProperty
and defaults to lambda x: None.
-
ext
¶ List of file extensions to be displayed in the manager. For example, [‘py’, ‘kv’] - will filter out all files, except python scripts and Kv Language.
ext
is anListProperty
and defaults to [].
-
search
¶ It can take the values ‘dirs’ ‘files’ - display only directories or only files. By default, it displays and folders, and files. Available options are: ‘all’, ‘files’.
search
is anOptionProperty
and defaults to all.
-
current_path
¶ Current directory.
current_path
is anStringProperty
and defaults to /.
-
use_access
¶ Show access to files and directories.
use_access
is anBooleanProperty
and defaults to True.
-
previous
¶ Shows only image previews.
previous
is anBooleanProperty
and defaults to False.
-
show
(self, path)¶ Forms the body of a directory tree.
- Parameters
path – The path to the directory that will be opened in the file manager.
-
count_ext
(self, path)¶
-
get_access_string
(self, path)¶
-
get_content
(self, path)¶ Returns a list of the type [[Folder List], [file list]].
-
close
(self)¶ Closes the file manager window.
-
select_dir_or_file
(self, path)¶ Called by tap on the name of the directory or file.
-
back
(self)¶ Returning to the branch down in the directory tree.
Called when a click on a floating button.
-