Kivy Carousel和Button Widgets

时间:2017-11-15 16:07:40

标签: python-3.x python-2.7 kivy kivy-language

感谢我上一篇文章中的一些朋友,我有一个成功工作的Kivy Carousel,现在我正在尝试添加一个"开始使用#34;轮播最后一页上的按钮类型,用户可以在单击时将用户带到主应用程序视图。

我觉得我需要实现像#34这样的逻辑;如果当前图片是' n' "仅将按钮应用于最后一个屏幕,但我不确定如何使其工作。我一直四处寻找解决方案,但却找不到任何与这个特定概念有关的东西..

这是我的旋转木马代码,谢谢!!:

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage

class CarouselApp(App):

    def build(self):

        carousel = Carousel(direction='right')

        #For each loop to take each image from directory and place in carousel
        for i in range(0, 3):
            src = "carousel/%s.png" % str(i)
            image = AsyncImage(source=src, allow_stretch=True)
            carousel.add_widget(image)

        return carousel

CarouselApp().run()

1 个答案:

答案 0 :(得分:1)

在您的轮播的最后一项上,您必须添加一个包含图片和按钮的布局,而不是仅添加图片:

带有boxlayout的

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.uix.image import AsyncImage
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class CarouselApp(App):

    def build(self):

        carousel = Carousel(direction='right')

        #For each loop to take each image from directory and place in carousel
        for i in range(0, 3):
            if i == 2:
                src = "carousel/%s.png" % str(i)
                button = Button(text='Get Started', on_press=self.get_started, size_hint_y=.1)
                image = AsyncImage(source=src, allow_stretch=True, size_hint_y=.9)
                layout = BoxLayout(orientation='vertical')
                layout.add_widget(image)
                layout.add_widget(button)
                carousel.add_widget(layout)
            else:
                src = "carousel/%s.png" % str(i)
                image = AsyncImage(source=src, allow_stretch=True)
                carousel.add_widget(image)
        return carousel

    def get_started(self, *args):
        print "let's start"

CarouselApp().run()

使用floatlayout

...
from kivy.uix.floatlayout import FloatLayout

...

class CarouselApp(App):
    def build(self):
        carousel = Carousel(direction='right')
        #For each loop to take each image from directory and place in carousel
        for i in range(0, 3):
            if i == 2:
                src = "carousel/%s.png" % str(i)
                button = Button(text='Get Started', on_press=self.get_started, size_hint=(.5,.1), pos_hint={'center_x': .5, 'y': .1})
                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)
                carousel.add_widget(layout)

...