Python | Carousel widget in Kivy using .kv file



Kivy — it is a platform independent GUI tool in Python. Since it can be run on Android, IOS, Linux, Windows, etc. It is mainly used to develop Android application, but that does not mean it cannot be used in desktop applications.

Carousel widgets:

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

To work with this widget, you need 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 In the example we create buttons in the application. In this we used the functions load_previous () and load_next () .

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

load_previous ()
Animate to the previous slide.

Implementation of the approach:

file main.py:

# Program to explain how to add a carousel to kiwi

 
# import cool module

import kivy 

 
# Your application base class inherits from the application class.
# app: always refers to your application instance

from kivy.app import App 

 
# this limits the kivy version ie
# below this version you cannot
# use the application or software

kivy.require ( `1.9.0`

  

 
# Carousel widget provides
# classic mobile carousel
# preview where you can swipe between slides

from kivy.uix.carousel import Carousel

  
# GridLayout arranges children in a matrix.
# It takes up available space and
# divides it into columns and rows,
# then adds widgets to the resulting “cells”.

from kivy.uix.gridlayout import GridLayout

 

 
# Create layout class

class Corousel ( GridLayout):

pass

 
# Create application 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 application

if __ name__ = = ` __main__` :

CorouselApp () .run ()

File Corousel.kv :

# Corousel. kv code file

 
# Create Corousel
& lt; Corousel & gt ;:

  

rows: 2

 

# Shows an identifier that differs for different pages

Label:

text: str ( id (root))

  

  # This button takes 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 return to the previous page

Button

text: `prev`

on_release:

root.parent.parent.load_previous ()

 < / p>

# load_next () is used to navigate to a new page

Button

text: ` next`

on_release:

root.parent.parent.load_next ()

Output: