Table Of Contents
- LottieWidget
LottieWidgetLottieWidget.apply_slot()LottieWidget.get_norm_image_size()LottieWidget.has_slot()LottieWidget.markersLottieWidget.on_state()LottieWidget.play_marker()LottieWidget.play_segment()LottieWidget.refresh()LottieWidget.reset_slot()LottieWidget.seek()LottieWidget.set_color()LottieWidget.set_image()LottieWidget.set_opacity()LottieWidget.set_quality()LottieWidget.set_text()LottieWidget.slots
LottieWidget¶
A Kivy widget for displaying and controlling Lottie animations.
Features
Lottie JSON loading from
source(local file path)Playback control via
state('play','pause','stop'),seek(), andprogress(0.0-1.0 normalised position)Standard image display properties:
fit_mode,color,norm_image_sizeRuntime DOM manipulation
Segments and markers - divide an animation into named or custom ranges and play them independently:
markers,play_segment(),play_marker()Slot overrides - change colours, opacity, text, and images on named slots at runtime without reloading the file:
slots,has_slot(),set_color(),set_opacity(),set_text(),set_image(),apply_slot(),reset_slot()Rendering quality - tune the fidelity of blur and shadow effects:
set_quality()
Basic usage
from kivy.uix.lottie import LottieWidget
w = LottieWidget(source='animation.json', size_hint=(None, None),
size=(400, 400), state='play')
KV:
LottieWidget:
source: 'animation.json'
fit_mode: 'contain'
state: 'play'
eos: 'loop'
on_load: print('ready', self.duration, 'seconds')
on_eos: print('loop!')
Slot colour override
w = LottieWidget(source='branded.json')
w.bind(on_load=lambda *a: w.set_color('brand_color', (0.2, 0.6, 1.0)))
New in version 3.0.0.
- class kivy.uix.lottie.LottieWidget(**kwargs)¶
Bases:
kivy.uix.widget.WidgetWidget that displays a Lottie animation.
The animation is rasterized to a
Textureby the configured Lottie provider (kivy.core.lottie). The texture is shared with the canvas drawing instructions added by the KV rule inkivy/data/style.kv- the same rule that drivesImageandSvgWidget.All DOM manipulation methods delegate transparently to the underlying
LottieBaseprovider.- apply_slot(slot_data)¶
Apply a raw slot override from a JSON string and return the slot handle.
Prefer the typed helpers (
set_color(),set_opacity(),set_text(),set_image()) for common cases. Use this method when you need to apply a slot structure that the helpers do not cover.- Parameters:
slot_data (str) – Slot override as a JSON string.
- Returns:
Slot handle (pass to
reset_slot()to remove), orNoneon failure or when no animation is loaded.- Return type:
int or None
- get_norm_image_size()¶
Return the display size of the animation after applying
fit_mode.Mirrors
get_norm_image_size()exactly.
- has_slot(slot_id)¶
Return
Trueif slot_id is present in the loaded animation.- Parameters:
slot_id (str) – Slot ID to check.
- Return type:
bool
- property markers¶
List of marker names in the loaded animation.
- Return type:
list[str]
- on_state(instance, value)¶
Kivy property event - sync the provider whenever
statechanges.
- play_marker(name)¶
Restrict playback to the named marker range.
After the call,
duration,total_frames, andpositionare updated to reflect the marker’s frame range so that progress reporting and EOS detection remain accurate.- Parameters:
name (str) – Marker name (see
markers).- Raises:
ValueError – If the marker does not exist.
- play_segment(begin, end)¶
Restrict playback to the frame range [begin, end].
After the call,
duration,total_frames, andpositionare updated to reflect the new segment so that progress reporting and EOS detection remain accurate.- Parameters:
begin (float) – First frame (inclusive).
end (float) – Last frame (inclusive).
- refresh()¶
Force an immediate re-render of the current frame.
- reset_slot(slot_id=None)¶
Remove a slot override or clear all overrides.
- Parameters:
slot_id (str or None) – Slot ID string, or
Noneto clear all.
- seek(percent)¶
Seek to percent (0.0-1.0) through the animation.
- Parameters:
percent (float) – Position as a fraction of the total duration.
- set_color(slot_id, color)¶
Override the fill colour of a slot.
- Parameters:
slot_id (str) – Slot ID.
color –
(r, g, b)or(r, g, b, a)floats 0.0-1.0.
- Returns:
Slot handle or
None.- Return type:
int or None
- set_image(slot_id, source)¶
Override the image of an image slot.
- Parameters:
slot_id (str) – Slot ID.
source (str) – Absolute path to the replacement image.
- Returns:
Slot handle or
None.- Return type:
int or None
- set_opacity(slot_id, opacity)¶
Override the opacity of a slot.
- Parameters:
slot_id (str) – Slot ID.
opacity (float) – 0.0 (transparent) - 1.0 (opaque).
- Returns:
Slot handle or
None.- Return type:
int or None
- set_quality(value)¶
Set the rendering quality level (0-100).
- Parameters:
value (int) – 0 = fastest, 100 = best.
- set_text(slot_id, text)¶
Override the text content of a text slot.
- Parameters:
slot_id (str) – Slot ID.
text (str) – New text string.
- Returns:
Slot handle or
None.- Return type:
int or None
- property slots¶
Frozenset of slot IDs in the loaded animation.
- Return type:
frozenset[str]