Version

Quick search

Accordion

New in version 1.0.8.

_images/accordion.jpg

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.

The Accordion should contain one or many AccordionItem instances, each of which should contain one root content widget. You’ll end up with a Tree something like this:

  • Accordion

    • AccordionItem

      • YourContent

    • AccordionItem

      • BoxLayout

        • Another user content 1

        • Another user content 2

    • AccordionItem

      • Another user content

The current implementation divides the AccordionItem into two parts:

  1. One container for the title bar

  2. One container for the content

The title bar is made from a Kv template. We’ll see how to create a new template to customize the design of the title bar.

Warning

If you see message like:

[WARNING] [Accordion] not have enough space for displaying all children
[WARNING] [Accordion] need 440px, got 100px
[WARNING] [Accordion] layout aborted.

That means you have too many children and there is no more space to display the content. This is “normal” and nothing will be done. Try to increase the space for the accordion or reduce the number of children. You can also reduce the Accordion.min_space.

Simple example

from kivy.uix.accordion import Accordion, AccordionItem
from kivy.uix.label import Label
from kivy.app import App


class AccordionApp(App):
    def build(self):
        root = Accordion()
        for x in range(5):
            item = AccordionItem(title='Title %d' % x)
            item.add_widget(Label(text='Very big content\n' * 10))
            root.add_widget(item)
        return root


if __name__ == '__main__':
    AccordionApp().run()

Customize the accordion

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')
class kivy.uix.accordion.Accordion(**kwargs)

Bases: kivy.uix.widget.Widget

Accordion class. See module documentation for more information.

add_widget(widget, *args, **kwargs)

Add a new widget as a child of this widget.

Parameters:
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.

New in version 1.0.5.

canvas: str, defaults to None

Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.

New in version 1.9.0.

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
exception kivy.uix.accordion.AccordionException

Bases: Exception

AccordionException class.

class kivy.uix.accordion.AccordionItem(**kwargs)

Bases: kivy.uix.floatlayout.FloatLayout

AccordionItem class that must be used in conjunction with the Accordion class. See the module documentation for more information.

add_widget(*args, **kwargs)

Add a new widget as a child of this widget.

Parameters:
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.

New in version 1.0.5.

canvas: str, defaults to None

Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.

New in version 1.9.0.

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
on_touch_down(touch)

Receive a touch down event.

Parameters:
touch: MotionEvent class

Touch received. The touch is in parent coordinates. See relativelayout for a discussion on coordinate systems.

Returns:

bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.

remove_widget(*args, **kwargs)

Remove a widget from the children of this widget.

Parameters:
widget: Widget

Widget to remove from our children list.

>>> from kivy.uix.button import Button
>>> root = Widget()
>>> button = Button()
>>> root.add_widget(button)
>>> root.remove_widget(button)