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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically