kivy
  1. kivy-screen-manager

Screen Manager - Screen Navigation in Kivy

Screen Manager is a powerful tool in Kivy for managing the navigation between different screens or windows in your application. With Screen Manager, you can easily switch from one screen to another, making your application more user-friendly. In this tutorial, we will learn how to use the Screen Manager in Python Kivy.

Syntax

The syntax for creating a Screen Manager is:

from kivy.uix.screenmanager import ScreenManager, Screen

# Create manager
manager = ScreenManager()

# Create screens
screen1 = Screen(name='screen1')
screen2 = Screen(name='screen2')

# Add screens to manager
manager.add_widget(screen1)
manager.add_widget(screen2)

# Navigate to screen2
manager.current = 'screen2'

Example

Let us create a simple application with two screens to understand how the Screen Manager works.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition

class ScreenOne(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_widget(Label(text="Welcome to Screen 1"))

class ScreenTwo(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.add_widget(Label(text="Welcome to Screen 2"))

class ScreenManagement(ScreenManager):
    pass

class DemoApp(App):
    def build(self):
        # Create the screen manager
        sm = ScreenManagement()

        # Add screens
        sm.add_widget(ScreenOne(name='screen_one'))
        sm.add_widget(ScreenTwo(name='screen_two'))

        return sm

if __name__ == '__main__':
    DemoApp().run()

Explanation

Here, we have created two screens, ScreenOne and ScreenTwo. In the build method, we have created the ScreenManager and added our screens to the ScreenManager using the add_widget method.

We have also defined the ScreenManagement class which will be used as the Screen Manager. We added the ScreenManagement class as the base class for our Screen Manager, and we did not define anything in this class, because we want to use the default behavior of the Kivy Screen Manager.

We then returned the ScreenManagement instance from the build() method, and ran the application.

Use

Screen Manager is useful in creating applications with multiple screens or windows. It allows you to easily switch between screens, manage transitions between screens, and stack screens on top of each other. Screen Manager provides the following useful features:

  • Screen transition animations like slide in, slide out, and fade.
  • Easy navigation between screens using the Screen Manager's current property.
  • Dynamic screen creation and removal using the add_widget() and remove_widget() methods.

Important Points

  • Each screen must have a unique name, which is used to navigate between screens.
  • Screen Manager provides useful attributes, such as transition, transition_duration, and default_transition, to customize screen transitions between screens.
  • We can also customize screen transitions by defining our own animation classes.

Summary

In this tutorial, we learned about the Screen Manager in Kivy, which is used for managing screens or windows in your application. We learned about the syntax of the Screen Manager and saw an example of creating a simple Screen Manager application. We discussed the features of Screen Manager and some important points to keep in mind while using it.

Published on: