Table of contents
1.
Introduction
2.
Carousel Widget in Kivy
3.
Implementation
3.1.
Basic Method
3.1.1.
Python program
3.1.2.
Kivy File
3.1.3.
App Output
4.
Frequently Asked Questions
4.1.
What are Kivy widgets?
4.2.
What is Kivy Uix, exactly?
4.3.
Is Kivy good for Android?
4.4.
What is Kivy's relative layout?
5.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Carousel Widget in Kivy using .kv File

Author Aman Thakur
0 upvote

Introduction

In this article, we will be covering Carousel Widget in Kivy. It is a Python using GUI tool that works on any platform provided by Kivy module python. We will also see how to use the .kv file in Kivy to develop the application's layout. This article assumes that you have been somewhat familiar with the Basics of Python otherwise it will be a bit difficult to understand it at first glance however you can still go through this article.

Carousel Widget in Kivy

The Carousel Widget provides a mobile-friendly carousel layout that allows you to swipe between slides. Any material may be added to the carousel, and it can move horizontally or vertically. The carousel can display pages in a loop or in a series.


Note: load_next(mode=’next’) Animate to the next slide. load_previous() Animate to the previous slide.

Implementation

The first step to getting started with ScrollView Widget is that you need first to import it.

from kivy.uix.carousel import Carousel

Basic Method

1. import kivy App
2. import Gridlayout
3. import Carousel
4. Create as much as widget class as needed
5. create the App class
6. return the widget/layout etc class
7. Create Carousel.kv file:

    7.1 Create button( or what is needed)
    7.2 Arrange the on_release / on_press function
8. Run an instance of the class

Python program

from kivy.app import App
from kivy.uix.carousel import Carousel
from kivy.lang import Builder
from kivy.uix.image import AsyncImage

class CarouselApp(App):
   def build(self):
       carousel = Carousel(direction='right')
       for i in range(4):
           src = [
               'https://images.unsplash.com/photo-1469474968028-56623f02e42e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=874&q=80',
               'https://images.unsplash.com/photo-1501854140801-50d01698950b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=975&q=80',
               'https://images.unsplash.com/photo-1447752875215-b2761acb3c5d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80',
               'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=871&q=80'
           ]
           for img in src:
               image = AsyncImage(source=img, allow_stretch=True)
               carousel.add_widget(image)

       return carousel


CarouselApp().run()
You can also try this code with Online Python Compiler
Run Code

Kivy File

<CarouselApp>:
   Carousel:
       direction: 'right'
       AsyncImage:
           source: 'https://images.unsplash.com/photo-1469474968028-56623f02e42e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=874&q=80'
       AsyncImage:
           source: 'https://images.unsplash.com/photo-1501854140801-50d01698950b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=975&q=80'
       AsyncImage:
           source: 'https://images.unsplash.com/photo-1447752875215-b2761acb3c5d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80'
       AsyncImage:
           source: 'https://images.unsplash.com/photo-1441974231531-c6227db76b6e?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=871&q=80'
You can also try this code with Online Python Compiler
Run Code

App Output

 

Frequently Asked Questions

What are Kivy widgets?

In Kivy, a Widget is the fundamental component of a graphical user interface. It comes with a canvas on which you may draw on the screen. It takes in information and responds to it.

 

What is Kivy Uix, exactly?

A module is kivy.uix. Widgets are part of the User Experience and are graphical user interface components. The kivy.uix module contains classes for creating and managing Widgets.

 

Is Kivy good for Android?

Kivy is a great tool for developing Android Apps. Kivy is an excellent tool for creating Android applications. The nicest thing about kivy is that it is cross-platform, which means that the same project can be used to create apps for iOS, Android, Windows, and OS X.

It does, however, have certain performance drawbacks (as do most cross-platform tools like unity, cocos etc).

 

What is Kivy's relative layout?

Relative Layout: This layout allows you to assign children relative coordinates. Use the FloatLayout if you want absolute placement. The RelativeLayout class is similar to the FloatLayout class, with the exception that its child widgets are positioned relative to the layout.

Conclusion 

If you have reached here that means, you really enjoyed this article. This article covers the implementation of the Carousel widget in kivy with code snippets and their use cases. You might be interested in articles such as Floating Layout in KivyCarousel Layout in KivyButton Action in Kivy, Button Color in Kivy and Slider widget in Kivy.

Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning!

Live masterclass