Python | Accordion in kivy using .kv file

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.

Accordion:

The Accordion widget is a form of menu where the options are stacked either vertically or horizontally and the item in focus (when touched) opens up to display its content.

It can contain many item instances, each of which should contain one root content widget.Yow will be end up like a tree.



The current implementation divides the AccordionItem into two parts:

  1. One container for the title bar(made from kv templet)
  2. One container for the content
Basic Approach:
1) import kivy
2) import kivyApp
3) import Accordian
4) Set minimum version(optional)
5) Create Accordion class
6) Create App class
7) create .kv file (name same as the app class)
8) return Layout/widget/Class(according to requirement)
9) Run an instance of the class

Implementation of the Approach:

.py file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# How to use Accordion in kivy using .kv file 
  
# Program to Show how to create a switch 
# 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'
  
# The Accordion widget is a form of menu
# where the options are stacked either vertically 
# or horizontally and the item in focus 
# (when touched) opens up to display its content.
from kivy.uix.accordion import Accordion
  
# Create the Accordion class
# Whose work is done in .kv file 
class Accor(Accordion):
    pass
  
# Create App class
class AccorApp(App):
    def build(self):
        return Accor()
  
# run the App
if __name__ == '__main__':
    AccorApp().run()

chevron_right


.kv file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# .kv file of the Accordion App file  
  
# Allow style to image
<MyImage@Image>:
    keep_ratio: False
    allow_stretch: True
  
# Use the different image to show usage of accordian
<Accor>:
    orientation: 'vertical'
    AccordionItem:
        title: 'HTML 5'
        MyImage:
            source: 'html.png'
    AccordionItem:
        title: 'CSS 3'
        MyImage:
            source: 'css.png'
    AccordionItem:
        title: 'JAVASCRIPT'
        MyImage:
            source: 'javascript.png'
    AccordionItem:
        title: 'NODE-JS'
        MyImage:
            source: 'node-js.png'
    AccordionItem:
        title: 'BOOTSTRAP'
        MyImage:
            source: 'bootstrap.png'

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.