Eka
Eka

Reputation: 15010

How to load image when the corresponding carousel slide is active?

This is a kivy python script with carousel I found in the web which I am trying to replicate.

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.factory import Factory
from kivy.uix.image import Image


class Example1(App):

    def build(self):
        carousel = Carousel(direction='right',loop='true')

        for i in range(1,5):
            src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
            #load images asynchronously
            image = Factory.AsyncImage(source=src, allow_stretch=True)          
            carousel.add_widget(image)
            print(i)
        return carousel

if __name__ == '__main__':
    Example1().run()

This downloads all the images at once which works well for smaller sized and lesser number of images. When I tried it in other larger number of images with consideraly larger size. It took a long time to even load those images to kivy app.

Is there a way that we can load the images one by one? Say when we run the kivy app instead of downloading all images together; the first slide of carousel should only download the first image and when we swipe left or right the corresponding slides image should download.

Upvotes: 1

Views: 760

Answers (1)

user4612771
user4612771

Reputation:

you can create a custom generator function to load the previous or the next slide when called which then downloads or loads the respective image. this a script in my app which automatically changes the screen after every 3 seconds that but i created the carousel in my kv file manually instead of calling them from a remote source.

from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
import threading
from kivy.clock import Clock

class StartScreen(Screen):
    ml = ObjectProperty(None)
    scroller = ObjectProperty(None)
    button = ObjectProperty(None)
    carousel = ObjectProperty(None)

    def caller(self):
        threading.Thread(target = self.call).start()

    def call(self):
        Clock.schedule_interval(self.changer, 3)

    def changer(self,*args):
        self.ids.carousel.load_next()

Upvotes: 1

Related Questions