如何在Kivy中返回布局内的小部件? [英] How to return a widget inside a Layout in Kivy?

查看:83
本文介绍了如何在Kivy中返回布局内的小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个非常简单的Kivy应用程序,其中有不同的布局.我需要将应用程序拆分为GridLayout(rows=2),这样我可以在屏幕顶部显示一个标题",并在屏幕其余部分显示一个carouselaccordion.

I made a very simple Kivy application in which I have different layouts. I need to split my app into a GridLayout(rows=2), this way I can have a "header" at the top of the screen, and a carousel or accordion in the rest of the screen.

我的问题是我不知道如何在布局内返回小部件.

My problem is that I can't figure out how to return my widget inside the layout.

这是我的代码:

class Logo(App):

    def build(self):
        layout = GridLayout(rows=2)
        layoutTop = FloatLayout()
        layoutMid = FloatLayout()

        logo = Image(source='imagine.png',size_hint=(.25,.25),pos=(30,380))
        titre = Label(text='#LeCubeMedia',font_size='40sp',pos=(0,280))
        ip = Label(text='192.168.42.1',font_size='25sp',pos=(250,280))

        carousel = Carousel(direction='right', loop = True, size_hint=(.5,.5),pos=(300,180))
        for i in range(2):
                src = "imagine.png"
                image = Factory.AsyncImage(source=src, allow_stretch=True)
                carousel.add_widget(image)
                Clock.schedule_interval(carousel.load_next, 1)
        return carousel ------> 1st Return

        layoutTop.add_widget(titre)
        layoutTop.add_widget(logo)
        layoutTop.add_widget(ip)
        layoutMid.add_widget(carousel)

        layout.add_widget(layoutTop)
        layout.add_widget(layoutMid)

        return layout ------> 2nd Return

if __name__ == '__main__':

    Logo().run()

如您所见,我需要那两个return才能在布局中显示我的轮播,但是这样,只有轮播会出现在我的应用中.如果我删除return carousel,它将无法滑动图像,有点像刷新布局,从而阻止轮播正常传递图像.

As you can see, I need those 2 return in order to display my carousel inside my layout, but this way only the carousel will appear in my app. If I delete the return carousel, it will fail at swiping Images, a bit like there's a layout refresh that prevent the carousel from passing images normally.

有什么想法可以重新构造代码以在布局中具有良好的轮播?

Any ideas how I can re-structure the code to have a good carousel inside my layout ?

推荐答案

大规模

已从GitHub下载最新版本,因为load_next问题已在此处解决. 运行以下代码将导致正确的预期行为.

Download the latest version from GitHub, as the load_next issue has been resolved there. Running the following code results into the proper intended behavior.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.image import Image
from kivy.factory import Factory
from kivy.clock import Clock

class Logo(App):

    def build(self):

        main_layout = GridLayout(cols=1, rows=2)
        top_row = GridLayout(cols=3, rows=1)
        bottom_row = GridLayout(cols=1)

        logo = Image(source='bird.jpg')
        title = Label(text='Just three birds.',font_size='40sp')
        ip = Label(text='tweet\ntweet\ntweet',font_size='20sp')

        carousel = Carousel(direction='right', loop=True, size_hint=(.5,.5),pos=(0,180))

        for i in range(1,4):
            src = "bird%s.jpg" % str(i)
            image = Factory.AsyncImage(source=src, allow_stretch=True)
            carousel.add_widget(image)
        Clock.schedule_interval(carousel.load_next, 1.0)

        top_row.add_widget(logo)
        top_row.add_widget(title)
        top_row.add_widget(ip)
        bottom_row.add_widget(carousel)

        main_layout.add_widget(top_row)
        main_layout.add_widget(bottom_row)

        return main_layout

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

确保将图像文件以及标签/文本更改为正在使用的图像文件.现在应该可以使用了.

Make sure you change the image files to the ones you are using as well as the labels/text. It should work now.

此处 中观看演示视频.

See the demo video here.

这篇关于如何在Kivy中返回布局内的小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆