Add Images to Carousel via loop in KV file...

Joined
Mar 5, 2025
Messages
7
Reaction score
0
I know that one can add images like this...

Code:
def build(self):
        carrusel = Carousel(direction='bottom')

        for n in range(1, 15):
            src = f"roxxane/{n}.jpg"
            image = AsyncImage(source=src, allow_stretch=True)
            carrusel.add_widget(image)

or kv string like this...

Code:
Carousel:
            direction: 'right'         
            Image:
                source: 'Images\Carousel\Jose-Cuervo.png'
            Image:
                 source: 'Images\Carousel\Absolut.png'

But how can I do this loop in the kv code syntax...instead of images one below the other...

I tried a function but not sure how to call this function in the kv file...

Code:
class Tester(App):
    def build(self):
        scr = Builder.load_string(Code)
        return scr
    def add_Img(self, *args):
        box = self.root.ids.box
        for i in range(2):
            str = 'Images\Carousel\{i}.png'
            box.add_widget(str(i))

CrossPosted HERE
 
Joined
Mar 5, 2025
Messages
7
Reaction score
0
So...I've tried the below ...It works but the slide functionality of the carousel does not work...
Only displays first image...


Code:
class EzLaunchScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
      
        img_path = ('D:\\Steven\Desktop\\Python App\\Images\\Carousel\\')
        # Funtion get images int carousel...
        layout = FloatLayout()
        self.background_image = Image(source='Images\launchScreen.png')
        self.carousel = Carousel()
        #self.slider = Slider()
        for img_file in os.listdir(img_path):
            if img_file.endswith(('.png', '.jpg', '.jpeg')):
                img = Image(source=os.path.join(img_path, img_file))
                self.carousel.add_widget(img)       
        layout.add_widget(self.background_image)
        layout.add_widget(self.carousel)
        #layout.add_widget(self.slider)
      
        self.add_widget(layout)

And string code...

Code:
<EzLaunchScreen>:
    brand_text: Brand_label 
    val_text: ToT_label
    slide_text: slider_label
    name: 'EzLaunch'
    FloatLayout:
        # This is the carousel library of brands...
        Carousel:
            id: carousel_widget       
            direction: 'right'

Anyone have any ideas...
 
Last edited:
Joined
Mar 5, 2025
Messages
7
Reaction score
0
This solves...
Code:
class EzLaunchScreen(Screen):     
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        img_path = ('D:\\Steven\Desktop\\Python App\\Images\\Carousel\\')
        # Funtion get images int carousel...
        layout = FloatLayout()
        self.background_image = Image(source='Images\launchScreen.png')
        carousel_widget = Carousel(direction ='right')


        for img_file in os.listdir(img_path):
            if img_file.endswith(('.png', '.jpg', '.jpeg')):
                img = Image(source=os.path.join(img_path, img_file))
                print(f"Adding image: {img_file}")
                carousel_widget.add_widget(img)       
        layout.add_widget(self.background_image)       
        layout.add_widget(carousel_widget)
        self.add_widget(layout)
 
Joined
Nov 2, 2024
Messages
12
Reaction score
0
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import Image
from kivy.lang import Builder

KV = """
Carousel:
id: carousel
"""

class MyApp(App):
def build(self):
root = Builder.load_string(KV)
images = ["img1.png", "img2.png", "img3.png"] # Add your image paths
for img in images:
root.ids.carousel.add_widget(Image(source=img))
return root

MyApp().run()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,300
Messages
2,571,537
Members
48,327
Latest member
GWEDalene9
Top