This is our eleventh article in Python Kivy, in this article we are going to learn How to Create FileChooser in Kivy. There are two ready-to-use widgets that provide views of the file system. each of these present the files and folders in a different style. they both provide for scrolling, selection and basic user interaction.
- The
FileChooserListView
displays file entries as text items in a vertical list, where folders can be collapsed and expanded. - The
FileChooserIconView
presents icons and text from left to right, wrapping them as required.
Kivy Crash Course Articles
1: Kivy Crash Course Introduction & Installation
2: Introduction to Kv Design Language
5: How to Create CheckBox in Kivy
6: Kivy Image And AsyncImage Example
7: How to Create Slider in Python Kivy
8: How to Create TextInput in Python Kivy
9: How to Play Video in Python Kivy
10: Python Kivy Creating ToggleButton
Also you can watch the complete video for this article
OK so first of all create a python file like this , and add these codes in that file. basically at the top we have created a class that extends from GridLayout, because we are going to add our FileChooser with ImageView in the grid layout. after that we have created our main class that extends from the App class, and we return our MyWidget class in their. basically i want when a user chose an image from the file system, i want to set the image in my window image view.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from kivy.app import App from kivy.uix.gridlayout import GridLayout class MyWidget(GridLayout): def selected(self, filename): try: self.ids.image.source = filename[0] except: pass class FileChooserWindow(App): def build(self): return MyWidget() if __name__ == "__main__": window = FileChooserWindow() window.run() |
Now let’s create our .kv file, i have named my kv file filechooserwindow.kv, make sure that this name should be similar to the main class name, in my case it is FileChooserWindow class. so you can see in the code that we have defined some rules for MyWidget class, because that class extends from the GridLayout and we need to specify the column with id for this GridLayout. after that we have used our two types of FileChooser, as i have said before, there two types of FileChooser that you can use, the first one is FileChooserListView and the second one is FileChooserIconView. after that we have created our Image, because we want to select an image and add that to our window.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#:kivy 1.10.0 <MyWidget>: cols:2 id:my_widget #FileChooserListView: FileChooserIconView: id:filechooser on_selection:my_widget.selected(filechooser.selection) Image: id:image source:"" |
So now run the complete code and this will be the result, in here we have used FileChooserIconView
Also you can use FileChooserListView instead of that, and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email
How we can add that selected image in new window