Python | Carousel Widget In Kivy using .kv file

Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.

Carousel widget:

The Carousel widget provides the classic mobile-friendly carousel view where you can swipe between slides. You can add any content to the carousel and have it move horizontally or vertically. The carousel can display pages in a sequence or a loop.

To work with this widget you must have to import:

from kivy.uix.carousel import Carousel
Basic Approach:
1) import kivy
2) import kivy App
3) import Gridlayout
4) import Carousel
5) set minimum version(optional)
6) Create as much as widget class as needed
7) create the App class
8) return the widget/layout etc class
9) Create Carousel.kv file:
        1) Create button( or what is needed)
        2) Arrange the on_release / on_press function
10) Run an instance of the class

In the below example we are creating the buttons in the App. In this we used load_previous() and load_next() functions.

load_next(mode=’next’)
Animate to the next slide.

load_previous()
Animate to the previous slide.

Implementation of the Approach:

main.py file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Program to explain how to add carousel in kivy 
    
# import kivy module    
import kivy  
      
# base Class of your App inherits from the App class.    
# app:always refers to the instance of your application   
from kivy.app import App 
    
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
kivy.require('1.9.0'
    
  
# The Carousel widget provides the
# classic mobile-friendly carousel
# view where you can swipe between slides
from kivy.uix.carousel import Carousel
  
# The GridLayout arranges children in a matrix. 
# It takes the available space and 
# divides it into columns and rows, 
# then adds widgets to the resulting “cells”.
from kivy.uix.gridlayout import GridLayout
  
  
# Create the Layout Class
class Corousel(GridLayout):
    pass
  
# Create the App class
class CorouselApp(App):
    def build(self):
        # Set carousel widget as root
        root = Carousel()
  
        # for multiple pages
        for x in range(10):
            root.add_widget(Corousel())
        return root
  
  
# run the App
if __name__ == '__main__':
    CorouselApp().run()

chevron_right


Corousel.kv file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Corousel.kv file of the code
  
# Corousel Creation
<Corousel>:
  
    rows: 2
  
    # It shows the id which is different for different pages
    Label:
        text: str(id(root))
  
    # This button will take you directly to the 3rd page    
    Button
        text: 'load(page 3)'
        on_release:
            carousel = root.parent.parent
            carousel.load_slide(carousel.slides[2])
  
    # load_previous() is used to go back to previous page
    Button
        text: 'prev'
        on_release:
            root.parent.parent.load_previous()
  
    # load_next() is used to go to nevt page
    Button
        text: 'next'
        on_release:
            root.parent.parent.load_next()

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.