Skip to content
Related Articles

Related Articles

Python | Accordion in kivy
  • Last Updated : 07 Feb, 2020

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.

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. You will 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

You can increase the default size of the title bar:



root = Accordion(min_space=60)

Or change the orientation to vertical:

root = Accordion(orientation=’vertical’)

The AccordionItem is more configurable and you can set your own title background when the item is collapsed or opened:

item = AccordionItem(background_normal=’image_when_collapsed.png’,
background_selected=’image_when_selected.png’)

Basic Approach:
1) import kivy
2) import kivyApp
3) import Accordian, AccordianItem
4) import Label
5) Create App class
6) return Layout/widget/Class(according to requirement)
7) Run an instance of the class

Implementation of the Approach:




# 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, AccordionItem
  
# Label is the text which we want
# to add on our window, give to
# the buttons and so on
from kivy.uix.label import Label
  
  
# Create the App class
class AccordionApp(App):
      
    def build(self):
        root = Accordion()
        root = Accordion(min_space = 60)
        # Providing the orentation
        root = Accordion(orientation ='vertical')
  
        # Adding text to each Accordion
        for x in range(5):
            item = AccordionItem(title ='Title % d' % x)
            item.add_widget(Label(text ='GFG is Good Website foe CSE Students\n' * 5))
            root.add_widget(item)
  
        # Reurn the root
        return root
  
# Run the App
if __name__ == '__main__':
    AccordionApp().run()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up