pythonkivyaccordion

Align title to the left in an AccordionItem


I am using an Accordion widget within my sample app (used for learning Kivy). In my KV code, I have:

    Accordion:
        orientation: 'vertical'

        AccordionItem:
            title: '2024/12/20  00:00    <reference>'
            ...

The title is displayed centered within the AccordionItem header. I would like to change this to have all the titles aligned to the left. But I didn't see a way to do this in the documentation for the Accordion widget.

Is there a simple way to do this simple thing???


Solution

  • According to the documentation, you can adjust the title_template which describes how the title is displayed. Try adding this to your kv file:

    [AccordionItemTitle@Label]:
        text: ctx.title
        halign: 'left'
        valign: 'center'
        text_size: self.size
        normal_background: ctx.item.background_normal if ctx.item.collapse else ctx.item.background_selected
        disabled_background: ctx.item.background_disabled_normal if ctx.item.collapse else ctx.item.background_disabled_selected
        canvas.before:
            Color:
                rgba: self.disabled_color if self.disabled else self.color
            BorderImage:
                source: self.disabled_background if self.disabled else self.normal_background
                pos: self.pos
                size: self.size
            PushMatrix
            Translate:
                xy: self.center_x, self.center_y
            Rotate:
                angle: 90 if ctx.item.orientation == 'horizontal' else 0
                axis: 0, 0, 1
            Translate:
                xy: -self.center_x, -self.center_y
        canvas.after:
            PopMatrix
    

    This just adds:

    halign: 'left'
    valign: 'center'
    text_size: self.size
    

    to the template. Which adjusts the title to do what you want. See the Label to understand what is happening.