File Manager¶
A simple manager for selecting directories and files.
Example¶
from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.modalview import ModalView
from kivymd.uix.filemanager import MDFileManager
from kivymd.theming import ThemeManager
from kivymd.toast import toast
Builder.load_string('''
<ExampleFileManager@BoxLayout>
orientation: 'vertical'
spacing: dp(5)
MDToolbar:
id: toolbar
title: app.title
left_action_items: [['menu', lambda x: None]]
elevation: 10
md_bg_color: app.theme_cls.primary_color
FloatLayout:
MDRoundFlatIconButton:
text: "Open manager"
icon: "folder"
pos_hint: {'center_x': .5, 'center_y': .6}
on_release: app.file_manager_open()
''')
class Example(MDApp):
title = "File Manage"
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.bind(on_keyboard=self.events)
self.manager_open = False
self.manager = None
def build(self):
return Factory.ExampleFileManager()
def file_manager_open(self):
if not self.manager:
self.manager = ModalView(size_hint=(1, 1), auto_dismiss=False)
self.file_manager = MDFileManager(
exit_manager=self.exit_manager, select_path=self.select_path)
self.manager.add_widget(self.file_manager)
self.file_manager.show('/') # output manager to the screen
self.manager_open = True
self.manager.open()
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.dismiss()
self.manager_open = False
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
¶
-
kivymd.uix.filemanager.
ACTIVITY_MANAGER
=¶
#:import os os #:import Window kivy.core.window.Window
- <BodyManager@BoxLayout>
icon: ‘folder’ path: ‘’ background_normal: ‘’ background_down: ‘’ dir_or_file_name: ‘’ access_string: ‘’ events_callback: lambda x: None orientation: ‘vertical’
- ModifiedOneLineIconListItem:
text: root.dir_or_file_name on_release: root.events_callback(root.path) IconFolder:
disabled: True icon: root.icon
MDSeparator:
- <LabelContent@MDLabel>
size_hint_y: None height: self.texture_size[1] shorten: True shorten_from: ‘center’ halign: ‘center’ text_size: self.width, None
- <BodyManagerWithPrevious>
paths: [] path: ‘’ type: ‘folder’ events_callback: lambda x: None
- GridLayout:
id: grid_box cols: 3 row_default_height: (self.width - self.cols*self.spacing[0])/self.cols row_force_default: True size_hint_y: None height: self.minimum_height padding: dp(4), dp(4) spacing: dp(4)
- BoxLayout:
orientation: ‘vertical’ IconButton:
mipmap: True source:
root.get_source( app, root.type, label_box_1, root.paths, 1, self)
- on_release:
root.events_callback( os.path.join(root.path, label_box_1.text))
- LabelContent:
id: label_box_1 text:
os.path.split(root.paths[0])[1].replace(‘thumb_’, ‘’) if len(root.paths) >= 1 else ‘’
- BoxLayout:
orientation: ‘vertical’ IconButton:
mipmap: True source:
root.get_source( app, root.type, label_box_2, root.paths, 2, self)
- on_release:
root.events_callback( os.path.join(root.path, label_box_2.text))
- LabelContent:
id: label_box_2 text:
os.path.split(root.paths[1])[1].replace(‘thumb_’, ‘’) if len(root.paths) >= 2 else ‘’
- BoxLayout:
orientation: ‘vertical’ IconButton:
mipmap: True source:
root.get_source( app, root.type, label_box_3, root.paths, 3, self)
- on_release:
root.events_callback( os.path.join(root.path, label_box_3.text))
- LabelContent:
id: label_box_3 text:
os.path.split(root.paths[2])[1].replace(‘thumb_’, ‘’) if len(root.paths) >= 3 else ‘’
- <FloatButton>
anchor_x: ‘right’ anchor_y: ‘bottom’ size_hint_y: None height: dp(56) padding: dp(10)
- MDFloatingActionButton:
size_hint: None, None size:dp(56), dp(56) icon: root.icon opposite_colors: True elevation: 8 on_release: root.callback() md_bg_color: root.md_bg_color
- <MDFileManager>
- canvas:
- Color:
- rgba:
1, 1, 1, 1
- Rectangle:
size: self.size pos: self.pos
- BoxLayout:
orientation: ‘vertical’ spacing: dp(5)
- MDToolbar:
id: toolbar title: ‘%s’ % root.current_path right_action_items: [[‘close-box’, lambda x: root.exit_manager(1)]] left_action_items: [[‘chevron-left’, lambda x: root.back()]] elevation: 10 md_bg_color: root.theme_cls.primary_color
- RecycleView:
id: rv key_viewclass: ‘viewclass’ key_size: ‘height’ bar_width: dp(4) bar_color: root.theme_cls.primary_color on_scroll_stop: root.update_list_images()
- RecycleBoxLayout:
padding: dp(10) default_size: None, dp(48) default_size_hint: 1, None size_hint_y: None height: self.minimum_height orientation: ‘vertical’
- <ModifiedBaseListItem>
size_hint_y: None canvas:
- Color:
- rgba:
self.theme_cls.divider_color if root.divider is not None else (0, 0, 0, 0)
- Line:
points: (root.x ,root.y, root.x+self.width, root.y) if root.divider == ‘Full’ else (root.x+root._txt_left_pad, root.y, root.x+self.width-root._txt_left_pad-root._txt_right_pad, root.y)
- BoxLayout:
id: _text_container orientation: ‘vertical’ pos: root.pos padding:
root._txt_left_pad, root._txt_top_pad, root._txt_right_pad, root._txt_bot_pad
- MDLabel:
id: _lbl_primary text: root.text font_style: root.font_style theme_text_color: root.theme_text_color size_hint_y: None shorten: True max_lines: 1 height: self.texture_size[1]
- <ModifiedOneLineIconListItem>
- BoxLayout:
id: _left_container size_hint: None, None x: root.x + dp(16) y: root.y + root.height/2 - self.height/2 size: dp(48), dp(48)
-
class
kivymd.uix.filemanager.
IconButton
¶ Bases:
kivymd.uix.behaviors.CircularRippleBehavior
,kivy.uix.behaviors.ButtonBehavior
,kivy.uix.image.AsyncImage
-
class
kivymd.uix.filemanager.
FloatButton
¶ Bases:
kivy.uix.anchorlayout.AnchorLayout
-
callback
¶
-
md_bg_color
¶
-
icon
¶
-
-
class
kivymd.uix.filemanager.
ModifiedBaseListItem
¶ Bases:
kivymd.theming.ThemableBehavior
,kivymd.uix.behaviors.RectangularRippleBehavior
,kivy.uix.behaviors.ButtonBehavior
,kivy.uix.floatlayout.FloatLayout
Base class to all ListItems. Not supposed to be instantiated on its own.
-
text
¶ Text shown in the first line.
text
is aStringProperty
and defaults to “”.
-
text_color
¶ Text color used if theme_text_color is set to ‘Custom’
-
font_style
¶
-
theme_text_color
¶ Theme text color for primary text
-
secondary_text
¶ Text shown in the second and potentially third line.
The text will wrap into the third line if the ListItem’s type is set to ‘one-line’. It can be forced into the third line by adding a n escape sequence.
secondary_text
is aStringProperty
and defaults to “”.
-
secondary_text_color
¶ Text color used for secondary text if secondary_theme_text_color is set to ‘Custom’
-
secondary_theme_text_color
¶ Theme text color for secondary primary text
-
secondary_font_style
¶
-
divider
¶
-
-
class
kivymd.uix.filemanager.
ModifiedOneLineListItem
(**kwargs)¶ Bases:
kivymd.uix.filemanager.ModifiedBaseListItem
A one line list item
-
class
kivymd.uix.filemanager.
ContainerSupport
¶ Overrides add_widget in a ListItem to include support for I*Body widgets when the appropiate containers are present.
-
add_widget
(self, widget, index=0)¶
-
remove_widget
(self, widget)¶
-
on_touch_down
(self, touch)¶
-
on_touch_move
(self, touch, *args)¶
-
on_touch_up
(self, touch)¶
-
propagate_touch_to_touchable_widgets
(self, touch, touch_event, *args)¶
-
-
class
kivymd.uix.filemanager.
ModifiedOneLineIconListItem
¶ Bases:
kivymd.uix.filemanager.ContainerSupport
,kivymd.uix.filemanager.ModifiedOneLineListItem
-
class
kivymd.uix.filemanager.
IconFolder
¶ Bases:
kivymd.uix.list.ILeftBodyTouch
,kivymd.uix.button.MDIconButton
-
class
kivymd.uix.filemanager.
BodyManagerWithPrevious
¶ Bases:
kivy.uix.boxlayout.BoxLayout
-
get_source
(self, app, source_type, instance_label, paths, index, instance_content)¶
-
-
class
kivymd.uix.filemanager.
MDFileManager
(**kwargs)¶ Bases:
kivymd.theming.ThemableBehavior
,kivy.uix.floatlayout.FloatLayout
-
icon
¶ The icon that will be used on the directory selection button.
-
exit_manager
¶ Function called when the user reaches directory tree root.
-
select_path
¶ Function, called when selecting a file/directory.
-
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.
-
search
¶ It can take the values ‘dirs’ ‘files’ - display only directories or only files. By default, it displays and folders, and files.
-
current_path
¶ Current directory.
-
use_access
¶ Show accec to files and directories.
-
previous
¶ Shows only image previews.
-
update_list_images
(self)¶
-
split_list
(self, l, n)¶
-
create_previous
(self, path)¶
-
check_theme
(self)¶
-
show
(self, path)¶ Forms the body of a directory tree.
-
count_ext
(self, path)¶
-
get_access_string
(self, path)¶
-
get_content
(self, path)¶ Returns a list of the type [[Folder List], [file list]].
-
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.
-