Change language

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
"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:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method