カルーセルウィジェット

2015/10/28

Python 2.7.9, Kivy 1.9.0

スワイプでスライドするウィジェット。かっこいいよね。

KvファイルでCarouselを使う

# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.gridlayout import GridLayout

from kivy.lang import Builder

kv = '''
Carousel:
    GridLayout
        cols: 3 
        Button
            text: 'prev'
            on_release:
                root.load_previous()
        Label:
            text: 'Page1'
        Button
            text: 'next'
            on_release:
                root.load_next()
    GridLayout
        cols: 3 
        Button
            text: 'prev'
            on_release:
                root.load_previous()
        Label:
            text: 'Page2'
        Button
            text: 'next'
            on_release:
                root.load_next()
    GridLayout
        cols: 3 
        Button
            text: 'prev'
            on_release:
                root.load_previous()
        Label:
            text: 'Page3'
        Button
            text: 'next'
            on_release:
                root.load_next()
'''

class TestApp(App):
    def build(self):
        return Builder.load_string(kv)

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


10ページのパターン

# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.gridlayout import GridLayout
from kivy.properties import StringProperty

from kivy.lang import Builder

Builder.load_string('''
<Page>:
    cols: 3

    Button
        text: 'prev'
        on_release:
            root.parent.parent.load_previous()

    Label:
        text: root.page_name

    Button
        text: 'next'
        on_release:
            root.parent.parent.load_next()
''')

class Page(GridLayout):
    page_name = StringProperty()

    def __init__(self,no):
        super(Page,self).__init__()
        self.page_name = 'Page ' + str(no)

class MyApp(App):
    def build(self):
        root = Carousel()
        for i in range(10):
            root.add_widget(Page(i+1))
        return root

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


kivy/carousel_buttons.py at master - kivy/kivy - GitHub
https://github.com/kivy/kivy/blob/master/examples/widgets/carousel_buttons.py