Rakig
Rakig

Reputation: 55

Button for switch screen inside Kivy carousel

I'm trying to create a button to change/return to the previous screen inside the carousel.

I am used to managing my buttons in the.kv file but it is not possible for the carousel, if I do that only one button is created on the last slide.

I need the button to be present on each slide, so I did this and it works well except I don't know how to link the button to the ScreenManager.

Part of my python file:

class MyCarousel(Carousel):
    def __init__(self, **kwargs):
        super(MyCarousel, self).__init__(**kwargs)
        self.direction = "right"
        log_file = open_logfile()
        for i in range(log_file['len']):
            src = log_file['path'][(log_file['len']) - (i + 1)] + "_file_resize.jpg"
            button = Button(size_hint=(.15,.25),\
                    pos_hint={'center_x': .05, 'y': .65},\
                    background_color=(1, 1, 1, 1),\
                    border=(0, 0, 0, 0),\
                    on_release=***???***
                    background_normal="Interface_PNG/bouton/Bouton_retour.png",\
                    background_down="Interface_PNG/bouton/Bouton_retour_fonce.png")
            image = AsyncImage(source=src, allow_stretch=True, size_hint=(1, 1), pos_hint={'center_x': .5, 'y': 0})
            layout = FloatLayout()
            layout.add_widget(image)
            layout.add_widget(button)
            self.add_widget(layout)
        self.loop = True

class StartScreen(Screen):
    pass

class GalleryPhoto(Screen):
    def call_next(self, dt):
        self.manager.current = "start"

class RootScreen(ScreenManager):
    pass

class PhotoboothApp(App):
    def build(self):
        sm = RootScreen()
        self.start_screen = StartScreen()
        self.gallery_photo = GalleryPhoto()
        sm.add_widget(self.start_screen)
        sm.add_widget(self.gallery_photo)
        return sm

Part of my kv file:

<StartScreen>:
    name: "start"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        Image:
            source: "Interface_PNG/Page_1.png"
            allow_stretch : True
            keep_ratio : False
            size_hint: 1, 1
        Button:
            background_color: 0, 0, 0, 0
            on_release: root.manager.current = "gallery"

<GalleryPhoto>:
    name: "gallery"
    FloatLayout:
        Image:
            source: "Interface_PNG/Page_8.png"
            allow_stretch: True
            keep_ratio: False
            size_hint: 1, 1
        MyCarousel:

I'd like to do something like that but in python:

 MyCarousel:
     Button:
        on_release: root.manager.current = "start"

I think it's simple but I'm a little lost... Any help is welcome.

Upvotes: 0

Views: 484

Answers (1)

ikolim
ikolim

Reputation: 16041

  • Implement a change_screen() method in class MyCarousel()
  • Bind button to change_screen() method
  • Use App.get_running_app().root to access the instantiated root, ScreenManager object.

Snippets - py

class MyCarousel(Carousel):
    def __init__(self, **kwargs):
        ...
            button = Button(size_hint=(.15,.25), pos_hint={'center_x': .05, 'y': .65},
                            background_color=(1, 1, 1, 1), border=(0, 0, 0, 0),
                            on_release=self.change_screen(),

            ...

    def change_screen(self, instance):
        App.get_running_app().root.current = 'start'

Example

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder


class MyCarousel(Carousel):
    def __init__(self, **kwargs):
        super(MyCarousel, self).__init__(**kwargs)
        self.direction = "right"

        for i in range(10):
            src = "http://placehold.it/480x270.png&text=slide-%d&.png" % i
            image = AsyncImage(source=src, allow_stretch=True, size_hint=(1, 1), pos_hint={'center_x': .5, 'y': 0})
            button = Button(size_hint=(.15, .25), pos_hint={'center_x': .05, 'y': .65},
                            text='StartScreen',
                            on_release=self.change_screen)

            layout = FloatLayout()
            layout.add_widget(image)
            layout.add_widget(button)
            self.add_widget(layout)
        self.loop = True

    def change_screen(self, instance):
        App.get_running_app().root.current = 'start'


class StartScreen(Screen):
    pass


class GalleryPhoto(Screen):
    pass


class RootScreen(ScreenManager):
    pass


Builder.load_file("main.kv")


class TestApp(App):
    title = "Photobooth"

    def build(self):
        return RootScreen()


if __name__ == "__main__":
    TestApp().run()

main.kv

<RootScreen>:
    StartScreen:
    GalleryPhoto:


<StartScreen>:
    name: "start"
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
    FloatLayout:
        Image:
            source: "kivy-logo.png"
            allow_stretch : True
            keep_ratio : True   # False
            size_hint: 1, 1
        Button:
            background_color: 0, 0, 0, 0
            on_release: root.manager.current = "gallery"

<GalleryPhoto>:
    name: "gallery"
    FloatLayout:
        Image:
            source: "kivymd_logo.png"
            allow_stretch: True
            keep_ratio: False
            size_hint: 1, 1
        MyCarousel:

Output

Before changing to StartScreen StartScreen

Upvotes: 1

Related Questions