Skip to content Skip to sidebar Skip to footer

Swapping Screens With Motion In Kivy

Is it possible to swap screens with motion, rather than on button press? I am asking in context of mobile applications, where this behaviour is often expected by users.

Solution 1:

You can use Carousel widget. For example:

fromkivy.appimportAppfromkivy.uix.boxlayoutimportBoxLayoutfromkivy.langimportBuilderBuilder.load_string("""<MyWidget>:Carousel:BoxLayout:orientation:'vertical'Label:text:"Screen 1"Label:text:"Some text"BoxLayout:orientation:'vertical'Label:text:"Screen 2"BoxLayout:Button:text:"1"Button:text:"2"Button:text:"3"Button:text:"4"BoxLayout:orientation:'vertical'Label:text:"Screen 3"Label:text:"Some other text"""")

class MyWidget(BoxLayout):
    pass

class MyApp(App):
    def build(self):
        return MyWidget()

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

Solution 2:

I think this might be a better way to do things:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.carousel import Carousel
from kivy.lang import Builder

Builder.load_string("""
<MyCarousel>:
    BoxLayout:
        orientation: 'vertical'

        Label:
            text: "Screen 1"
        Label:
            text: "Some text"

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: "Screen 2"
        BoxLayout:
            Button:
                text: "1"
            Button:
                text: "2"
            Button:
                text: "3"
            Button:
                text: "4"

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: "Screen 3"
        Label:
            text: "Some other text"
""")

classMyCarousel(Carousel):
    passclassMyApp(App):
    defbuild(self):
        return MyCarousel()

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

Apparently, a Carousel doesn't behave like other widgets as it doesn't have a default size of 100, 100.

Post a Comment for "Swapping Screens With Motion In Kivy"