Table Of Contents
FileChooser¶
New in version 1.0.5.
Changed in version 1.2.0: In the chooser template, the controller is not a direct reference anymore but a weak-reference. You must update all the notation root.controller.xxx to root.controller().xxx.
Simple example¶
main.py
#!/usr/bin/env python
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
import os
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class SaveDialog(FloatLayout):
save = ObjectProperty(None)
text_input = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
loadfile = ObjectProperty(None)
savefile = ObjectProperty(None)
text_input = ObjectProperty(None)
def dismiss_popup(self):
self._popup.dismiss()
def show_load(self):
content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def show_save(self):
content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
self._popup = Popup(title="Save file", content=content, size_hint=(0.9, 0.9))
self._popup.open()
def load(self, path, filename):
with open(os.path.join(path, filename[0])) as stream:
self.text_input.text = stream.read()
self.dismiss_popup()
def save(self, path, filename):
with open(os.path.join(path, filename), 'w') as stream:
stream.write(self.text_input.text)
self.dismiss_popup()
class Editor(App):
pass
Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)
if __name__ == '__main__':
Editor().run()
editor.kv
#:kivy 1.1.0
Root:
text_input: text_input
BoxLayout:
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 30
Button:
text: 'Load'
on_release: root.show_load()
Button:
text: 'Save'
on_release: root.show_save()
BoxLayout:
TextInput:
id: text_input
text: ''
RstDocument:
text: text_input.text
show_errors: True
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
<SaveDialog>:
text_input: text_input
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
on_selection: text_input.text = self.selection and self.selection[0] or ''
TextInput:
id: text_input
size_hint_y: None
height: 30
multiline: False
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Save"
on_release: root.save(filechooser.path, text_input.text)
- class kivy.uix.filechooser.FileChooserListView(**kwargs)[source]¶
Bases: kivy.uix.filechooser.FileChooserController
Implementation of FileChooserController using a list view.
- class kivy.uix.filechooser.FileChooserIconView(**kwargs)[source]¶
Bases: kivy.uix.filechooser.FileChooserController
Implementation of FileChooserController using an icon view.
- class kivy.uix.filechooser.FileChooserController(**kwargs)[source]¶
Bases: kivy.uix.floatlayout.FloatLayout
Base for implementing a FileChooser. Don’t use this class directly, but prefer using an implementation such as the FileChooserListView or FileChooserIconView.
Events: - on_entry_added: entry, parent
Fired when a root-level entry is added to the file list.
- on_entries_cleared
Fired when the the entries list is cleared, usually when the root is refreshed.
- on_subentry_to_entry: entry, parent
Fired when a sub-entry is added to an existing entry.
- on_remove_subentry: entry, parent
Fired when entries are removed from an entry, usually when a node is closed.
- on_submit: selection, touch
Fired when a file has been selected with a double-tap.
- cancel(*largs)[source]¶
Cancel any background action started by filechooser, such as loading a new directory.
New in version 1.2.0.
- dirselect¶
BooleanProperty, defaults to False. Determines whether directories are valid selections or not.
New in version 1.1.0.
- entry_released(entry, touch)[source]¶
(internal) This method must be called by the template when an entry is touched by the user.
New in version 1.1.0.
- entry_touched(entry, touch)[source]¶
(internal) This method must be called by the template when an entry is touched by the user.
- file_encodings¶
Possible encodings for decoding a filename to unicode. In the case that the user has a weird filename, undecodable without knowing it’s initial encoding, we have no other choice than to guess it.
Please note that if you encounter an issue because of a missing encoding here, we’ll be glad to add it to this list.
New in version 1.3.0.
Deprecated since version 1.8.0: This property is no longer used as the filechooser no longer decodes the file names.
file_encodings is a ListProperty and defaults to [‘utf-8’, ‘latin1’, ‘cp1252’],
- file_system¶
Implementation to access the file system. Must be an instance of FileSystemAbstract.
New in version 1.8.0.
ObjectProperty, defaults to FileSystemLocal()
- files¶
Read-only ListProperty. The list of files in the directory specified by path after applying the filters.
- filter_dirs¶
BooleanProperty, defaults to False. Indicates whether filters should also apply to directories.
- filters¶
ListProperty, defaults to [], equal to ‘*’. Specifies the filters to be applied to the files in the directory.
The filters are not reset when the path changes. You need to do that yourself if desired.
There are two kinds of filters: patterns and callbacks.
Patterns
e.g. [‘*.png’]. You can use the following patterns:
Pattern Meaning * matches everything ? matches any single character [seq] matches any character in seq [!seq] matches any character not in seq Callbacks
You can specify a function that will be called for each file. The callback will be passed the folder and file name as the first and second parameters respectively. It should return True to indicate a match and False otherwise.
Changed in version 1.4.0: If the filter is a callable (function or method), it will be called with the path and the file name as arguments for each file in the directory. The callable should returns True to indicate a match and False overwise.
- get_nice_size(fn)[source]¶
Pass the filepath. Returns the size in the best human readable format or ‘’ if it is a directory (Don’t recursively calculate size.).
- multiselect¶
BooleanProperty, defaults to False. Determines whether the user is able to select multiple files or not.
- path¶
StringProperty, defaults to the current working directory as a unicode string. It specifies the path on the filesystem that this controller should refer to.
Warning
If a unicode path is specified, all the files returned will be in unicode allowing the display of unicode files and paths. If a bytes path is specified, only files and paths with ascii names will be displayed properly: non-ascii filenames will be displayed and listed with questions marks (?) instead of their unicode characters.
- progress_cls¶
Class to use for displaying a progress indicator for filechooser loading.
New in version 1.2.0.
ObjectProperty, defaults to FileChooserProgress.
Changed in version 1.8.0: If you set a string, the Factory will be used to resolve the class.
- rootpath¶
Root path to use instead of the system root path. If set, it will not show a ”..” directory to go up to the root path. For example, if you set rootpath to /users/foo, the user will be unable to go to /users or to any other directory not starting with /users/foo.
New in version 1.2.0.
StringProperty, defaults to None.
Note
Similar to path, if rootpath is specified, whether it’s a bytes or unicode string determines the type of the filenames and paths read.
- selection¶
Read-only ListProperty. Contains the list of files that are currently selected.
BooleanProperty, defaults to False. Determines whether hidden files and folders should be shown.
- sort_func¶
ObjectProperty. Provides a function to be called with a list of filenames, and the filesystem implementation as the second argument. Returns a list of filenames sorted for display in the view.
Changed in version 1.8.0: The signature needs now 2 arguments: first the list of files, second the filesystem class to use.
- class kivy.uix.filechooser.FileChooserProgressBase(**kwargs)[source]¶
Bases: kivy.uix.floatlayout.FloatLayout
Base for implementing a progress view. This view is used when too many entries need to be created and are delayed over multiple frames.
New in version 1.2.0.
- path¶
Current path of the FileChooser, read-only.
- total¶
Total number of entries to load.
- class kivy.uix.filechooser.FileSystemAbstract[source]¶
Bases: object
Class for implementing a File System view that can be used with the FileChooser.:attr:~FileChooser.file_system.
New in version 1.8.0.
Return True if the file is hidden
- class kivy.uix.filechooser.FileSystemLocal[source]¶
Bases: kivy.uix.filechooser.FileSystemAbstract
Implementation of FileSystemAbstract for local files
New in version 1.8.0.