- Kivy Tutorial
- Kivy - Home
- Kivy Basics
- Kivy - Getting Started
- Kivy - Installation
- Kivy - Architecture
- Kivy - File Syntax
- Kivy - Applications
- Kivy - Hello World
- Kivy - App Life Cycle
- Kivy - Events
- Kivy - Properties
- Kivy - Inputs
- Kivy - Behaviors
- Kivy Buttons
- Kivy - Buttons
- Kivy - Button Events
- Kivy - Button Colors
- Kivy - Button Size
- Kivy - Button Position
- Kivy - Round Buttons
- Kivy - Disabled Buttons
- Kivy - Image Button
- Kivy Widgets
- Kivy - Widgets
- Kivy - Label
- Kivy - Text Input
- Kivy - Canvas
- Kivy - Line
- Kivy - Checkbox
- Kivy - Dropdown List
- Kivy - Windows
- Kivy - ScrollView
- Kivy - Carousel
- Kivy - Slider
- Kivy - Images
- Kivy - Popup
- Kivy - Switch
- Kivy - Spinner
- Kivy - Splitter
- Kivy - Progress Bar
- Kivy - Bubble
- Kivy - Tabbed Panel
- Kivy - Scatter
- Kivy - Accordion
- Kivy - File Chooser
- Kivy - Color Picker
- Kivy - Code Input
- Kivy - Modal View
- Kivy - Toggle Button
- Kivy - Camera
- Kivy - Tree View
- Kivy - reStructuredText
- Kivy - Action Bar
- Kivy - Video Player
- Kivy - Stencil View
- Kivy - VKeyboard
- Kivy - Touch Ripple
- Kivy - Audio
- Kivy - Videos
- Kivy - Spelling
- Kivy - Effects
- Kivy - Input Recorder
- Kivy - OpenGL
- Kivy - Text
- Kivy - Text Markup
- Kivy - Settings
- Kivy Layouts
- Kivy - Layouts
- Kivy - Float Layout
- Kivy - Grid Layouts
- Kivy - Box Layouts
- Kivy - Stack Layout
- Kivy - Anchor Layout
- Kivy - Relative Layout
- Kivy - Page Layout
- Kivy - Recycle Layout
- Kivy - Layouts in Layouts
- Kivy Advanced Concepts
- Kivy - Configuration Object
- Kivy - Atlas
- Kivy - Data Loader
- Kivy - Cache Manager
- Kivy - Console
- Kivy - Animation
- Kivy - Multistroke
- Kivy - Clock
- Kivy - SVGs
- Kivy - UrlRequest
- Kivy - Clipboard
- Kivy - Factory
- Kivy - Gesture
- Kivy - Language
- Kivy - Graphics
- Kivy - Drawing
- Kivy - Packaging
- Kivy - Garden
- Kivy - Storage
- Kivy - Vector
- Kivy - Utils
- Kivy - Inspector
- Kivy - Tools
- Kivy - Logger
- Kivy - Framebuffer
- Kivy Applications and Projects
- Kivy - Drawing App
- Kivy - Calculator App
- Kivy - Stopwatch App
- Kivy - Camera Handling
- Kivy - Image Viewer
- Kivy - Bezier
- Kivy - Canvas Stress
- Kivy - Circle Drawing
- Kivy - Widget Animation
- Kivy - Miscellaneous
- Kivy Useful Resources
- Kivy - Quick Guide
- Kivy - Useful Resources
- Kivy - Discussion
Kivy - File Chooser
In a GUI application, you are often required to choose a required file from the local filesystem. Kivy framework presents the "kivy.uix.filechooser" module that provides various classes for describing, displaying and browsing file systems.
The classes in filechooser module adopt a MVC design. They can be categorized as follows −
Models are represented by concrete implementations of the FileSystemAbstract class, such as the FileSystemLocal.
Views are represented by the FileChooserListLayout and FileChooserIconLayout classes. These are used by the FileChooserListView and FileChooserIconView widgets respectively.
Controllers are represented by concrete implementations of the FileChooserController, namely the FileChooser, FileChooserIconView and FileChooserListView classes.
The FileChooserIconView and FileChooserListView classes provide very-simple-to-use widgets that provide access to the file system with two different visual representations, as the names suggest.
The FileChooserListView widget displays files and folders as text items in a vertical list. A folder is identified by a ">" sign to the left of its name, and a click expands or collapses its files and subfolders.
The FileChooserIconView widget displays a folder icon with the name below it, and a file icon with its name.
If the number of files/folders exceeds the height and width of the widget, the vertical and horizontal scrollbars are attached to it.
The FileChooser views have following properties −
files − The list of files in the directory specified by path after applying the filters. files is a read-only ListProperty.
filter_dirs − Indicates whether filters should also apply to directories. filter_dirs is a BooleanProperty and defaults to False.
filters − filters specifies the filters to be applied to the files in the directory. filters is a ListProperty and defaults to []. If empty, it is equivalent to '*' indicating that no file has been filtered out of the list. The filters are not reset when the path changes. You need to do that yourself if desired.
You can specify one or more of the following patterns in the list −
Sr.No | Patterns List & Description |
---|---|
1 | * matches everything |
2 | ? matches any single character |
3 | [seq] matches any character in seq |
4 | [!seq] matches any character not in seq |
Both the views raise the on_selection event to which a callback can be bound. When this event is generated, if a folder is selected, it will expand or collapse it. If a file is selected, its name is passed to the callback.
ListView Example
In the first example, we construct the App window with a FileChhoserListView widget and a label in a vertical box layout.
Use the following "kv" file script for the purpose. The select() method is bound to the view's "on_selection" event.
<Filechooser>: label: label orientation: 'vertical' BoxLayout: FileChooserListView: canvas.before: Color: rgb: .4, .5, .5 Rectangle: pos: self.pos size: self.size on_selection: root.select(*args) Label: id: label size_hint_y: .1 canvas.before: Color: rgb: .5, .5, .4 Rectangle: pos: self.pos size: self.size
Note the use of canvas object of FileChooseListView and Label widgets to provide background color.
The Kivy Application class code is given below −
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.core.window import Window Window.size = (720,400) class Filechooser(BoxLayout): def select(self, *args): try: self.label.text = args[1][0] except: pass class FileDemoApp(App): def build(self): return Filechooser() if __name__ == '__main__': FileDemoApp().run()
Output
When the above code is run, you get the list of files/folders. The name of the file with its full path is displayed on the label.
IconView Example
We now show the use of FileChooserIconView. It is placed in a horizontal box, along with an Image widget by its side. While most of the configuration of FileChooserIconView is same as in previous example, we add the filters property to restrict the display of files to png files only.
<Filechooser>: img: img orientation: 'horizontal' BoxLayout: FileChooserIconView: filters: ['*.png'] canvas.before: Color: rgb: .5, .4, .5 Rectangle: pos: self.pos size: self.size on_selection: root.select(*args) Image: id: img source:""
Since we intend to display the selected image, the select() method is changed so that the source property of Image object is assigned with the selected file.
class Filechooser(BoxLayout): def select(self, *args): try: self.img.source = args[1][0] except: print ('error') class FileIconApp(App): def build(self): return Filechooser() # run the App if __name__ == '__main__': FileIconApp().run()
Output
Run the program and browse to the desired image file. The selected image will be displayed on the right.
To Continue Learning Please Login
Login with Google