Skip to content

Accordion

Bases: Frame

A container of Expander widgets with optional mutual exclusion.

The Accordion manages a group of Expanders, optionally enforcing that only one can be expanded at a time. When allow_multiple=False, expanding one section automatically collapses the others. When allow_collapse_all=False, at least one section must remain open.

Attributes:

Name Type Description
expanders list[Expander]

List of managed Expander widgets.

expanded list[Expander]

Currently expanded Expander(s).

Events

  • <<AccordionChange>>: Fired when the expanded section(s) change. event.data = {'expanded': list[Expander]}

expanded property

expanded: list[Expander]

Get the list of currently expanded Expanders.

__init__

__init__(
    master: Master = None,
    *,
    allow_multiple: bool = False,
    allow_collapse_all: bool = True,
    show_separators: bool = False,
    accent: str = None,
    variant: str = None,
    **kwargs,
)

Create an Accordion widget.

Parameters:

Name Type Description Default
master Master

Parent widget. If None, uses the default root window.

None
allow_multiple bool

If True, multiple sections can be open at once. If False (default), only one section can be open at a time.

False
allow_collapse_all bool

If True (default), all sections can be collapsed. If False, at least one section must remain open.

True
show_separators bool

If True, show separators between expanders.

False
accent str

Accent token for the expanders (e.g., 'success', 'primary').

None
variant str

Variant for the expanders (e.g., 'solid', 'default').

None
**kwargs

Additional arguments passed to Frame.

{}

add

add(
    expander: Expander = None,
    *,
    key: str = None,
    title: str = "",
    icon: str | dict = None,
    expanded: bool = None,
    **kwargs,
) -> Expander

Add an expander to the accordion.

Parameters:

Name Type Description Default
expander Expander | None

Existing Expander to add. If None, creates one.

None
key str | None

Unique identifier for the expander. Auto-generated if not provided.

None
title str

Title for the expander header (when creating new).

''
icon str | dict

Icon for the expander header (when creating new).

None
expanded bool | None

Initial expansion state. If None, first expander is expanded when allow_collapse_all=False, otherwise collapsed.

None
**kwargs

When expander is None, passed to Expander constructor.

{}

Returns:

Name Type Description
Expander Expander

The added or created Expander widget.

Raises:

Type Description
ValueError

If an expander with the same key already exists.

remove

remove(key: str) -> None

Remove an expander from the accordion.

Parameters:

Name Type Description Default
key str

The key of the expander to remove.

required

Raises:

Type Description
KeyError

If no expander with the given key exists.

Note

The expander widget is destroyed. If allow_collapse_all=False and removing would leave no expanders, or would remove the only open expander, another expander will be expanded automatically.

item

item(key: str) -> Expander

Get an expander by its key.

Parameters:

Name Type Description Default
key str

The key of the expander to retrieve.

required

Returns:

Type Description
Expander

The Expander instance.

Raises:

Type Description
KeyError

If no expander with the given key exists.

items

items() -> tuple[Expander, ...]

Get all expander widgets in order.

Returns:

Type Description
tuple[Expander, ...]

A tuple of all Expander instances in the order they were added.

keys

keys() -> tuple[str, ...]

Get all expander keys in order.

Returns:

Type Description
tuple[str, ...]

A tuple of all expander keys in the order they were added.

configure_item

configure_item(key: str, option: str = None, **kwargs: Any)

Configure a specific expander by its key.

Parameters:

Name Type Description Default
key str

The key of the expander to configure.

required
option str

If provided, return the value of this option.

None
**kwargs Any

Configuration options to apply to the expander.

{}

Returns:

Type Description

If option is provided, returns the value of that option.

expand

expand(key: str)

Expand the expander with the given key.

Parameters:

Name Type Description Default
key str

Key of the expander to expand.

required

collapse

collapse(key: str)

Collapse the expander with the given key.

Parameters:

Name Type Description Default
key str

Key of the expander to collapse.

required
Note

If allow_collapse_all=False and this is the only open expander, this call will be ignored.

expand_all

expand_all()

Expand all expanders (only effective when allow_multiple=True).

collapse_all

collapse_all()

Collapse all expanders (only effective when allow_collapse_all=True).

on_accordion_changed

on_accordion_changed(callback: Callable) -> str

Bind callback to <<AccordionChange>> events.

Parameters:

Name Type Description Default
callback Callable

Function to call when expanded sections change. Receives event with event.data = {'expanded': list[Expander]}.

required

Returns:

Type Description
str

Bind ID that can be passed to off_accordion_changed to remove this callback.

off_accordion_changed

off_accordion_changed(bind_id: str = None)

Unbind <<AccordionChange>> callback(s).

Parameters:

Name Type Description Default
bind_id str | None

Bind ID returned by on_accordion_changed. If None, unbinds all.

None