Python | Accordion in kivy
Last Updated :
04 Mar, 2022
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:
- One container for the title bar(made from kv template)
- 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 Accordion, AccordionItem
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:
Python3
import kivy
from kivy.app import App
kivy.require( '1.9.0' )
from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.label import Label
class AccordionApp(App):
def build( self ):
root = Accordion()
root = Accordion(min_space = 60 )
root = Accordion(orientation = 'vertical' )
for x in range ( 5 ):
item = AccordionItem(title = 'Title % d' % x)
item.add_widget(Label(text = 'GFG is Good Website for CSE Students\n' * 5 ))
root.add_widget(item)
return root
if __name__ = = '__main__' :
AccordionApp().run()
|
Output:
Share your thoughts in the comments
Please Login to comment...