External button code to get carousel image attribute...

Joined
Mar 5, 2025
Messages
7
Reaction score
0
Hi guys & gals...

So I have this code that populates my Carousel with images from the path....

Python Code snippet...
Python:
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)

And in string snippet I have the following...

Code:
BoxLayout:       
        # This is the carousel library of brands...
        Carousel:
            id: carousel_widget       
            direction: 'right'
            on_index: root.on_carousel_swipe()

I want a function that can be called to get the name of the image as I swipe on the carousel to be printed...
This is what i have that does not work...Do i need to bind this somewhere...

Python:
# Funtion get image name
    def on_carousel_swipe(carousel_widget, instance, value):
        current_image = carousel_widget.ids.carousel_widget.slides[int(value)]
        img_name = current_image.source.split('\\')[-1]
        img_name = img_name.split('.')[0]
        print (img_name)

Thus far for the past 5 days I have posted other queries in many other forums and sadly no responses received...

I'm hoping this forum is more forthcoming...
 
Joined
Mar 5, 2025
Messages
7
Reaction score
0
Cross posted HERE


Even ChatGPT's solution does not work...
Not much direction available for this....

Python:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.lang import Builder
kv = '''
<EzLaunchScreen>:
    name: 'EzLaunch'
    BoxLayout:
        orientation: 'vertical'
        Carousel:
            id: carousel
            direction: 'left'
            loop: True
            on_slide: root.on_slide(*args)
            Image:
                source: 'Images\Carousel\Bains.png'
            Image:
                source:  'Images\Carousel\Jose Cuervo.png'
            Image:
                source:  'Images\Carousel\Absolut.png'
                
'''

class EzLaunchScreen(Screen):
    def on_slide(self, instance, value):
        # Get the index of the current slide (value is float, so we round it)
        slide_index = int(value)
        
        # Get the image name (source) of the current slide
        image_name = self.ids.carousel.slides[slide_index].source
        print(f"Current Image: {image_name}")

sm = ScreenManager()
sm.add_widget(EzLaunchScreen(name='EzLaunch'))
class MyApp(App):
    def build(self):
        return Builder.load_string(kv)
    
if __name__ == '__main__':
    MyApp().run()
 
Last edited:
Joined
Mar 29, 2025
Messages
48
Reaction score
0
from kivy.uix.button import Button
from kivy.uix.carousel import Carousel
from kivy.uix.image import Image
from kivy.app import App

class MyApp(App):
def build(self):
self.carousel = Carousel()
images = ['image1.jpg', 'image2.jpg', 'image3.jpg']

for img in images:
self.carousel.add_widget(Image(source=img))

button = Button(text="Get Current Image")
button.bind(on_press=self.get_current_image)

return self.carousel

def get_current_image(self, instance):
current_image = self.carousel.slides[self.carousel.index]
print(f"Current image: {current_image.source}")

if name == "main":
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,342
Messages
2,571,407
Members
48,797
Latest member
kenne2025

Latest Threads

Top