Python | Accordion in kiw

Python Methods and Functions

Kivy — it is a platform independent GUI tool in Python. Since it can run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean that it cannot be used in desktop applications.


The Accordion widget is a form of menu in which options are arranged vertically or horizontally, and the element in focus (when touched) opens to display its contents. 
It can contain many element instances, each of which must contain one root content widget. You end up as a tree.

The current implementation splits the AccordionItem into two parts:

  1. One container for the title line (from kv templet)
  2. One container for content

You can increase the default header size:

root = Accordion (min_space = 60)

Or change the orientation to vertical:

root = Accordion (orientation = 'vertical')

The AccordionItem is more customizable and you can set your own header background when item collapsed or open:

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 the kiw accordion using a .kv file

# Program to show how to create a radio button
# import nodded module

import kivy 

# Your application base class inherits from the application class.
# app: always refers to your application instance

from import App 

# this limits the kivy version. e.
# below this version you cannot
# use the application or software

kivy.require ( '1.9.0'

# The accordion widget is a menu form
# where options are stacked vertically
# or horizontally and the element has focus
# (on tap) opens to display its content.

from kivy.uix.accordion import Accordion, AccordionItem

# The tag is text which we want
# add to our window, give
 # buttons and so on

from kivy.uix.label import Label


# Create application class

class AccordionApp (App):


def build ( self ):

root = Accordion ()

root = Accordion (min_space =  60 )

# Provide orientation

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' * 5 ))

  root.add_widget (item)


# Return root

return root

# Run the application

if __ name__ = = ' __main__' :

AccordionApp (). Run ()

Exit :

Get Solution for free from DataCamp guru