Version

Quick search

Asynchronous data loader

This is the Asynchronous Loader. You can use it to load an image and use it, even if data are not yet available. You must specify a default loading image when using the loader:

from kivy.loader import Loader
image = Loader.image('mysprite.png')

You can also load an image from a url:

image = Loader.image('http://mysite.com/test.png')

If you want to change the default loading image, you can do:

Loader.loading_image = Image('another_loading.png')

Tweaking the asynchronous loader

New in version 1.6.0.

You can tweak the loader to provide a better user experience or more performance, depending of the images you are going to load. Take a look at the parameters:

  • Loader.num_workers - define the number of threads to start for loading images.

  • Loader.max_upload_per_frame - define the maximum image uploads in GPU to do per frame.

class kivy.loader.LoaderBase

Bases: builtins.object

Common base for the Loader and specific implementations. By default, the Loader will be the best available loader implementation.

The _update() function is called every 1 / 25.s or each frame if we have less than 25 FPS.

image(filename, load_callback=None, post_callback=None, **kwargs)

Load a image using the Loader. A ProxyImage is returned with a loading image. You can use it as follows:

from kivy.app import App
from kivy.uix.image import Image
from kivy.loader import Loader

class TestApp(App):
    def _image_loaded(self, proxyImage):
        if proxyImage.image.texture:
            self.image.texture = proxyImage.image.texture

    def build(self):
        proxyImage = Loader.image("myPic.jpg")
        proxyImage.bind(on_load=self._image_loaded)
        self.image = Image()
        return self.image

TestApp().run()

In order to cancel all background loading, call Loader.stop().

pause()

Pause the loader, can be useful during interactions.

New in version 1.6.0.

resume()

Resume the loader, after a pause().

New in version 1.6.0.

run(*largs)

Main loop for the loader.

start()

Start the loader thread/process.

stop()

Stop the loader thread/process.

class kivy.loader.ProxyImage(arg, **kwargs)

Bases: kivy.core.image.Image

Image returned by the Loader.image() function.

Properties:
loaded: bool, defaults to False

This value may be True if the image is already cached.

Events:
on_load

Fired when the image is loaded or changed.

on_error

Fired when the image cannot be loaded. error: Exception data that occurred