simplegui_lib_loader — class to load images and sounds

(Version saved in CodeSkulptor http://www.codeskulptor.org/#user40_nMs7JxzimyImAv2.py .)

Examples of use in :

simplegui_lib_loader (May 31, 2015)

A class to help load images and sounds in SimpleGUI of CodeSkulptor.

Piece of SimpleGUICS2Pygame. https://bitbucket.org/OPiMedia/simpleguics2pygame

GPLv3 — Copyright (C) 2013, 2014, 2015 Olivier Pirson http://www.opimedia.be/

class SimpleGUICS2Pygame.simplegui_lib_loader.Loader(frame, progression_bar_width, after_function, max_waiting=5000)[source]

Help to load images and sounds from Internet and wait finished.

With SimpleGUICS2Pygame, SimpleGUICS2Pygame.load_image() and SimpleGUICS2Pygame.load_sound() wait automatically until loading is completed.

But in CodeSkulptor, the browser load images and sounds asynchronously. (With SimpleGUI it is impossible to verify that the sounds are loaded. So Loader begin load sounds, and next begin load images. It wait each image is loaded, and considers that all downloads are completed.)

__init__(frame, progression_bar_width, after_function, max_waiting=5000)[source]

Set an empty loader.

Parameters:
  • frame – simplegui.Frame
  • progression_bar_width – (int or float) >= 0
  • after_function – function () -> *
  • max_waiting – (int or float) >= 0
_draw_loading(canvas)[source]

Draw waiting message on the canvas when images and sounds loading.

Parameters:canvas – simplegui.Canvas
add_image(url, name=None)[source]

Add an image from url and give it a name.

Execute `Loader.load()` before use images.

If name == None then “filename” of url is used.

Example: If url == ‘http://commondatastorage.googleapis.com/codeskulptor-assets/lathrop/asteroid_blue.png’ and name == None then ‘asteroid_blue.png’ is used.

Parameters:
  • url – str
  • name – None or str
  • wait – bool
add_sound(url, name=None)[source]

Add a sound from url and give it a name.

Execute `Loader.load()` before use sounds.

If name == None then “filename” of url is used.

Example: If url == ‘http://commondatastorage.googleapis.com/codeskulptor-assets/Epoq-Lepidoptera.ogg’ and name == None then ‘Epoq-Lepidoptera.ogg’ is used.

Parameters:
  • url – str
  • name – None or str
  • wait – bool
cache_clear()[source]
  • In standard Python with SimpleGUICS2Pygame: Empty the cache of Pygame surfaces used by each image of this Loader. See Image._pygamesurfaces_cached_clear .
  • In SimpleGUI of CodeSkulptor: do nothing.
get_image(name)[source]

If an image named name exist then return it, else return None

Parameters:name – str
Raise:Exception if Loader.load() was not executed since the addition of this image.
Returns:None or simplegui.Image
get_nb_images()[source]

Return the number of images (loaded or not).

Returns:int >= 0
get_nb_images_loaded()[source]

Return the number of loaded images.

It is the number of begin loading by Loader.load() and fully completed.

Returns:int >= 0
get_nb_sounds()[source]

Return the number of sounds (loaded or not).

Returns:int >= 0
get_nb_sounds_loaded()[source]

Return the number of loaded sounds.

It is the number of begin loading by Loader.load(), but not necessarily completed. Because with SimpleGUI of CodeSkulptor it is impossible to verify that the sounds are loaded.

Returns:int >= 0
get_sound(name)[source]

If a sound named name exist then return it, else return None

Parameters:name – str
Raise:Exception if load() was not executed since the addition of this sound.
Returns:None or simplegui.Sound
load()[source]

Start loading of all images and sounds added since last Loader.load() execution.

  • In standard Python with SimpleGUICS2Pygame: draw a progression bar on canvas and wait until the loading is finished.
  • In SimpleGUI of CodeSkulptor: don’t wait.
pause_sounds()[source]

Pause all sounds.

print_stats_cache()[source]
  • In standard Python with SimpleGUICS2Pygame: Print to stderr some statistics of cached Pygame surfaces used by each image of this Loader. See Image._print_stats_cache .
  • In SimpleGUI of CodeSkulptor: do nothing.
wait_loaded()[source]

Draw a progression bar on canvas and wait until all images and sounds are fully loaded. Then execute self._after_function.

After self._max_waiting milliseconds, abort and execute self._after_function.

See details in get_nb_sounds_loaded() documentation.

__weakref__

list of weak references to the object (if defined)

[source]