Python | File chooser in kivy

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

Filechooser:

The FileChooser module provides various classes for describing, displaying and browsing file systems. It is Simple like the My computer from where we can choose any file in the system.

  • 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.

Note: The both above points provide for scrolling, selection and basic user interaction.



Basic Approach 
1) import kivy
2) import kivyApp
3) import Boxlayot
4) Set minimum version(optional)
5) create layout class
6) create App class
7) create .kv file
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class or App

Implementation of the Approach:

.py file

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to explain how to use File chooser in kivy  
        
# import kivy module     
import kivy   
          
# base Class of your App inherits from the App class.     
# app:always refers to the instance of your application    
from kivy.app import App  
        
# this restrict the kivy version i.e   
# below this kivy version you cannot   
# use the app or software   
kivy.require('1.9.0')  
  
# BoxLayout arranges widgets in either in
# a vertical fashion that is one on top of
# another or in a horizontal fashion
# that is one after another. 
from kivy.uix.boxlayout import BoxLayout
  
# create the layout class
class Filechooser(BoxLayout):
    def select(self, *args):
        try: self.label.text = args[1][0]
        except: pass
  
# Create the App class
class FileApp(App):
    def build(self):
        return Filechooser()
  
# run the App
if __name__ == '__main__':
    FileApp().run()

chevron_right


.kv file

filter_none

edit
close

play_arrow

link
brightness_4
code

#, kv file implementation
  
<Filechooser>:
      
    label: label
  
    # Providing the orentation
    orientation: 'vertical'
  
    # Creating the File list / icon view
      
    BoxLayout:
  
        # Creating list view one side
        FileChooserListView:
            canvas.before:
                Color:
                    rgb: .4, .5, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
  
        # Creating Icon view other side
        FileChooserIconView:
            canvas.before:
                Color:
                    rgb: .5, .4, .5
                Rectangle:
                    pos: self.pos
                    size: self.size
            on_selection: root.select(*args)
  
    # Adding label
    Label:
        id: label
        size_hint_y: .1
        canvas.before:
            Color:
                rgb: .5, .5, .4
            Rectangle:
                pos: self.pos
                size: self.size
         

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.