Table Of Contents
- SvgWidget
AsyncSvgWidgetSvgWidgetSvgWidget.get_element_ids()SvgWidget.get_element_opacity()SvgWidget.get_norm_image_size()SvgWidget.hide_element()SvgWidget.is_element_visible()SvgWidget.reload()SvgWidget.reset_element_overrides()SvgWidget.set_element_opacity()SvgWidget.set_element_visible()SvgWidget.show_element()SvgWidget.texture_update()
SvgWidget¶
A Kivy widget for displaying SVG content using adaptive rasterization via
the kivy.core.svg provider system.
Features
SVG loading from
source(local file path or in-memory SVGbytes;AsyncSvgWidgetadditionally accepts HTTP/HTTPS URLs)Efficient downscaling using mipmaps (enabled by default)
Re-rasterization at higher resolution when the display scale crosses an internal threshold
Limited runtime document customisation:
current_color- sets the CSScurrentColorvalueelement_overrides- per-element visibility and opacity by SVG elementid
AsyncSvgWidgetadds asynchronous HTTP/HTTPS loading
Difference from the SVG image provider
The SVG image provider (kivy.core.image) lets you use
Image(source="file.svg") and renders once at a fixed size.
SvgWidget differs in:
Adaptive re-rasterization - re-renders when scaling up beyond threshold
Mipmap-first - mipmaps enabled by default
``current_color`` - CSS
currentColorinjection not available via the image pipelineElement overrides - per-element visibility/opacity not available via the image pipeline
Basic usage
from kivy.uix.svg import SvgWidget
svg = SvgWidget(source='diagram.svg', size_hint=(None, None), size=(64, 64))
KV:
SvgWidget:
source: 'diagram.svg'
current_color: 1, 0, 0 # red currentColor
on_load: print('SVG ready')
AsyncSvgWidget:
source: 'https://example.com/diagram.svg'
SVG icon button
Combine ToggleButtonBehavior with
SvgWidget to create a tappable icon that changes colour on toggle.
This pattern works with any SVG that uses stroke="currentColor" or
fill="currentColor". Most popular svg icon libraries use currentColor by
default, including Lucide, Heroicons, and Tabler Icons.
# Python
from kivy.uix.behaviors import ToggleButtonBehavior
from kivy.uix.svg import SvgWidget
class StarIconToggleButton(ToggleButtonBehavior, SvgWidget):
pass
# KV
<StarIconToggleButton>:
source: 'star.svg'
size_hint: None, None
size: '64dp', '64dp'
fit_mode: 'contain'
mipmap: False
current_color: 'gold' if self.activated else 'grey'
New in version 3.0.0.
- class kivy.uix.svg.AsyncSvgWidget(**kwargs)¶
Bases:
kivy.uix.svg.SvgWidgetAsynchronous variant of
SvgWidgetthat downloads SVG files from HTTP/HTTPS URLs in a background thread.Local file paths are loaded synchronously (same behaviour as
SvgWidget). Remote URLs are downloaded in a daemon thread; the widget showsloading_texturewhile the download is in progress.Note
The loading image and error image are taken from Kivy’s global
Loader.loading_imageandLoader.error_image, matchingAsyncImage. To use a custom image, set these before creating anyAsyncSvgWidget:: The loading image supports GIF animation.from kivy.loader import Loader Loader.loading_image = ‘my_spinner.gif’ Loader.error_image = ‘my_error.png’
Example KV usage:
AsyncSvgWidget: source: 'https://example.com/icon.svg' on_load: print('downloaded and ready') on_error: print('failed:', args[1])
- get_norm_image_size()¶
Return the display size after applying
fit_mode.While loading or showing an error placeholder the texture is drawn at its natural size (
scale-downbehaviour) regardless offit_mode, so a small spinner GIF is never stretched to fill the widget. Once the SVG is ready the base class logic applies.
- static is_uri(filename)¶
Return
Trueif filename is an HTTP or HTTPS URL.Non-string values (e.g.
bytes,pathlib.Path) always returnFalse.- Parameters:
filename – Source value to test.
- Return type:
bool
- texture_update(*largs)¶
No-op - prevents the inherited synchronous load path.
- class kivy.uix.svg.SvgWidget(**kwargs)¶
Bases:
kivy.uix.widget.WidgetWidget that displays an SVG document using adaptive rasterization.
The SVG is rasterized to a
Texturevia thekivy.core.svgprovider. The texture is re-created when the display size grows beyond_RERENDER_THRESHOLDtimes the current raster size, keeping the image sharp at larger scales without wasting GPU memory at smaller ones.Downscaling is handled efficiently by OpenGL mipmaps (
mipmapdefaults toTrue).- get_element_ids()¶
Return a list of all SVG element
idvalues in the document.The list is built once at load time. Returns an empty list if the SVG has not been loaded yet, and logs a warning.
- Return type:
list[str]
- get_element_opacity(element_id)¶
Return the current opacity override for element_id.
Returns
1.0for elements with no opacity override.- Parameters:
element_id (str) – The SVG element
id.- Return type:
float
- get_norm_image_size()¶
Return the display size of the SVG after applying
fit_mode.Mirrors
get_norm_image_size()exactly.
- hide_element(element_id)¶
Hide the SVG element with the given
id.Convenience wrapper for
set_element_visible(element_id, False).- Parameters:
element_id (str) – The SVG element
idattribute value.
- is_element_visible(element_id)¶
Return whether the element with element_id is currently visible.
Returns
Truefor elements with no override (SVG default).- Parameters:
element_id (str) – The SVG element
id.- Return type:
bool
- reload()¶
Discard the current SVG document and reload from
source.Resets
loadedandstatusimmediately; the new texture is available after the next successful rasterization.
- reset_element_overrides()¶
Clear all element visibility and opacity overrides.
Triggers a re-render that restores all elements to their SVG defaults.
- set_element_opacity(element_id, opacity)¶
Set the opacity override for an SVG element by its
id.Stores the override in
element_overridesand triggers a re-render. If the SVG is not yet loaded, the override is still stored.If element_id is not found in the document, a warning is logged.
- Parameters:
element_id (str) – The SVG element
id.opacity (float) – Opacity value 0.0 (transparent) - 1.0 (opaque).
- set_element_visible(element_id, visible)¶
Set the visibility of an SVG element by its
id.Stores the override in
element_overridesand triggers a re-render. If the SVG is not yet loaded, the override is still stored and will be applied when the document loads.If element_id is not found in the document, a warning is logged but the override is still stored.
- Parameters:
element_id (str) – The SVG element
id.visible (bool) –
Trueto show,Falseto hide.
- show_element(element_id)¶
Show the SVG element with the given
id.Convenience wrapper for
set_element_visible(element_id, True).- Parameters:
element_id (str) – The SVG element
idattribute value.
- texture_update(*largs)¶
Load (or reload) the SVG from
sourceand rasterize.Called automatically when
sourcechanges. Can also be called manually to force a reload.