1

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.

Here's my code :

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()

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 ?

hacks4life
  • 691
  • 5
  • 16
  • 38
  • Hello, can you kindly post your entire code for this one? Off the top of my head, the lone answer so far is correct, you should only return once. However, we'll try to remedy that by fixing the above if we can. However, for that, I need the whole code, if the above is not yet all that. – WGS Apr 08 '14 at 14:29
  • The issue has been resolved after @Tshirtman pushed the fix. Try updating your Kivy to the master on GitHub. Setting `schedule_interval` on the `load_next` function now works. – WGS Apr 09 '14 at 14:53

2 Answers2

1

Just remove the return carousel line, you can only return one time, so you need to return the widget that contains all the others.

Also you put the Clock.schedule_interval call in the loop, so there is at much call each second that there is elements, consequence is that it will do a complete loop no matter what. only do this call one time, so move it out of the loop.

Tshirtman
  • 5,859
  • 1
  • 19
  • 26
  • I tried, but I have this strange behaviour regarding my carousel when I do not `return carousel`. Seems to be frozen when executed. – hacks4life Apr 08 '14 at 13:28
  • What you had was a bug with carousel's load_next, i pushed commit 6ef8c4 to fix it. – Tshirtman Apr 08 '14 at 14:25
  • So you mean if I import uix.carousel now I won't have this problem anymore? – hacks4life Apr 09 '14 at 13:40
  • If you updated your kivy version, you shouldn't have this problem anymore, if you can't update kivy, use min_move=.1 in the Carousel() call to workaround the bug. – Tshirtman Apr 09 '14 at 20:13
  • How to update my kivy version ? Do I just have to download the zip on GitHub and then "sudo python setup.py install" ? – hacks4life Apr 10 '14 at 08:10
1

MASSIVE EDIT:

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.

WGS
  • 13,969
  • 4
  • 48
  • 51
  • Hey Nanashi, thank you for the complete answer. Actually I've added the Clock method in your codee to let the carousel swipe by itself and I still have the same problem. Without a `return carousel` it does not swipe normally. Too bad I can't post a short 3sec video to let you see how it actually acts, because it's a very strange behaviour. – hacks4life Apr 09 '14 at 13:38
  • We have to wait until the commit Tshirtman pushed is resolved, and we'll possibly get a fix on it on the next update. Honestly, you cannot return two things in one go. It's not the way functions should work. I will be testing this further. – WGS Apr 09 '14 at 14:02
  • How can I get the latest version from GitHub ? Do I just have to download the zip on GitHub and then "sudo python setup.py install" ? – hacks4life Apr 10 '14 at 08:10
  • 1
    That's one option. Another is to clone it. Third is to use `pip`. Check out the answers on [this SO post](http://stackoverflow.com/questions/15268953/how-to-install-python-package-from-github). – WGS Apr 10 '14 at 13:43