Friday, December 27, 2024
Google search engine
HomeLanguagesPython | File chooser in kivy

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.

Kivy Tutorial – Learn Kivy with Examples.

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 Boxlayout
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 

Python3




# 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()


.kv file 

Python3




#, kv file implementation
 
<Filechooser>:
     
    label: label
 
    # Providing the orientation
    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
        


Output: 

 

 

RELATED ARTICLES

Most Popular

Recent Comments