Table of contents
1.
Introduction
2.
Screen Manager Widget in Kivy
3.
Implementation
3.1.
Basic Method
3.1.1.
Python program
3.1.2.
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

Screen Manager in Kivy

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

One must be thinking that I am a python developer. Still, I also want to develop apps for Android and I don’t want to learn any other framework or library such as flutter, Android Native or swift for developing IOS applications. No worries, pal; we'll be studying the Kivy framework supplied by Python, which will take you on a tour of constructing applications in Python quickly and effortlessly.

In this article, we will be covering ScreenManager in Kivy a Python using GUI tool that works on any platform provided by the 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.

Screen Manager Widget in Kivy

The ScreenManager is a widget that allows you to manage numerous displays in your programme. The default ScreenManager only shows one Screen at a time and switches between them using a TransitionBase.

Changing the screen coordinates/scale or even executing complex animation using custom shaders are supported for several transitions.

Implementation

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

from kivy.uix.screenmanager import ScreenManager, Screen
You can also try this code with Online Python Compiler
Run Code

Basic Method

After importing all the required modules  follow the algorithm listed below:

1 import kivyApp
2 import Screen Manager, Screen, ""Transitions you want to use""
3 import Builder from kivy.lang “ To take input from file or given doc string” 
4 Create Different Screen classes and pass them
5 Create features of Screen classes in .kv file 
       5.1 Add features on different screens
6 Create App class
7 return screen manager
8 Run an instance of the class
 

Note: The default ScreenManager.transition is a SlideTransition with options for direction and duration.

Python program

# import kivy app class
from kivy.app import App
# import app builder which build the .kv file or can also be used 
# for using doc string to lay out the layout of our app
from kivy.lang import Builder
# importing the screen manager
from kivy.uix.screenmanager import (ScreenManager, Screen, NoTransition, SlideTransition, CardTransition, SwapTransition, FadeTransition, WipeTransition, FallOutTransition, RiseInTransition)

# You can create your kv code in the Python file
Builder.load_string("""
<ScreenOne>:
   BoxLayout:
       Button:
           text: "Go to Screen 2"
           background_color : 0, 0, 1, 1
           on_press:
               root.manager.transition.direction = 'left'
               root.manager.transition.duration = 1
               root.manager.current = 'screen_two'

<ScreenTwo>:
   BoxLayout:
       Button:
           text: "Go to Screen 3"
           background_color : 1, 1, 0, 1
           on_press:
               root.manager.transition.direction = 'left'
               root.manager.transition.duration = 1
               root.manager.current = 'screen_three'

<ScreenThree>:
   BoxLayout:
       Button:
           text: "Go to Screen 4"
           background_color : 1, 0, 1, 1
           on_press:
               root.manager.transition.direction = 'left'
               root.manager.transition.duration = 1
               root.manager.current = 'screen_four'

<ScreenFour>:
   BoxLayout:
       Button:
           text: "Go to Screen 5"
           background_color : 0, 1, 1, 1
           on_press:
               root.manager.transition.direction = 'left'
               root.manager.transition.duration = 1
               root.manager.current = 'screen_five'

<ScreenFive>:
   BoxLayout:
       Button:
           text: "Go to Screen 1"
           background_color : 1, 0, 0, 1
           on_press:
               root.manager.transition.direction = 'right'
               root.manager.current = 'screen_one'


""")

# Create a class for all screens in which you can include
# helpful methods specific to that screen
class ScreenOne(Screen):
   pass

class ScreenTwo(Screen):
   pass

class ScreenThree(Screen):
   pass

class ScreenFour(Screen):
   pass

class ScreenFive(Screen):
   pass

# creating the screen manger object
screen_manager = ScreenManager(transition = RiseInTransition())

screen_manager.add_widget(ScreenOne(name ="screen_one"))
screen_manager.add_widget(ScreenTwo(name ="screen_two"))
screen_manager.add_widget(ScreenThree(name ="screen_three"))
screen_manager.add_widget(ScreenFour(name ="screen_four"))
screen_manager.add_widget(ScreenFive(name ="screen_five"))

# Inheriting the property of App class in ScreenApp
class ScreenApp(App):
   def build(self):
       return screen_manager

# Running main
if __name__ == '__main__':
   sample_app = ScreenApp()
   sample_app.run()
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 Screen Manager 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