Version

Quick search

Screen Manager

_images/screenmanager.gif

New in version 1.4.0.

The screen manager is a widget dedicated to managing multiple screens for your application. The default ScreenManager displays only one Screen at a time and uses a TransitionBase to switch from one Screen to another.

Multiple transitions are supported based on changing the screen coordinates / scale or even performing fancy animation using custom shaders.

Basic Usage

Let’s construct a Screen Manager with 4 named screens. When you are creating a screen, you absolutely need to give a name to it:

from kivy.uix.screenmanager import ScreenManager, Screen

# Create the manager
sm = ScreenManager()

# Add few screens
for i in range(4):
    screen = Screen(name='Title %d' % i)
    sm.add_widget(screen)

# By default, the first screen added into the ScreenManager will be
# displayed. You can then change to another screen.

# Let's display the screen named 'Title 2'
# A transition will automatically be used.
sm.current = 'Title 2'

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

Please note that by default, a Screen displays nothing: it’s just a RelativeLayout. You need to use that class as a root widget for your own screen, the best way being to subclass.

Warning

As Screen is a RelativeLayout, it is important to understand the Common Pitfalls.

Here is an example with a ‘Menu Screen’ and a ‘Settings Screen’:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Goto settings'
            on_press: root.manager.current = 'settings'
        Button:
            text: 'Quit'

<SettingsScreen>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
""")

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

class TestApp(App):

    def build(self):
        # Create the screen manager
        sm = ScreenManager()
        sm.add_widget(MenuScreen(name='menu'))
        sm.add_widget(SettingsScreen(name='settings'))

        return sm

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

Changing Direction

A common use case for ScreenManager involves using a SlideTransition which slides right to the next screen and slides left to the previous screen. Building on the previous example, this can be accomplished like so:

Builder.load_string("""
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Goto settings'
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'settings'
        Button:
            text: 'Quit'

<SettingsScreen>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press:
                root.manager.transition.direction = 'right'
                root.manager.current = 'menu'
""")

Advanced Usage

From 1.8.0, you can now switch dynamically to a new screen, change the transition options and remove the previous one by using switch_to():

sm = ScreenManager()
screens = [Screen(name='Title {}'.format(i)) for i in range(4)]

sm.switch_to(screens[0])
# later
sm.switch_to(screens[1], direction='right')

Note that this method adds the screen to the ScreenManager instance and should not be used if your screens have already been added to this instance. To switch to a screen which is already added, you should use the current property.

Changing transitions

You have multiple transitions available by default, such as:

  • NoTransition - switches screens instantly with no animation

  • SlideTransition - slide the screen in/out, from any direction

  • CardTransition - new screen slides on the previous or the old one slides off the new one depending on the mode

  • SwapTransition - implementation of the iOS swap transition

  • FadeTransition - shader to fade the screen in/out

  • WipeTransition - shader to wipe the screens from right to left

  • FallOutTransition - shader where the old screen ‘falls’ and becomes transparent, revealing the new one behind it.

  • RiseInTransition - shader where the new screen rises from the screen centre while fading from transparent to opaque.

You can easily switch transitions by changing the ScreenManager.transition property:

sm = ScreenManager(transition=FadeTransition())

Note

Currently, none of Shader based Transitions use anti-aliasing. This is because they use the FBO which doesn’t have any logic to handle supersampling. This is a known issue and we are working on a transparent implementation that will give the same results as if it had been rendered on screen.

To be more concrete, if you see sharp edged text during the animation, it’s normal.

class kivy.uix.screenmanager.CardTransition[source]

Bases: kivy.uix.screenmanager.SlideTransition

Card transition that looks similar to Android 4.x application drawer interface animation.

It supports 4 directions like SlideTransition: left, right, up and down, and two modes, pop and push. If push mode is activated, the previous screen does not move, and the new one slides in from the given direction. If the pop mode is activated, the previous screen slides out, when the new screen is already on the position of the ScreenManager.

New in version 1.10.

mode

Indicates if the transition should push or pop the screen on/off the ScreenManager.

  • ‘push’ means the screen slides in in the given direction

  • ‘pop’ means the screen slides out in the given direction

mode is an OptionProperty and defaults to ‘push’.

start(manager)[source]

(internal) Starts the transition. This is automatically called by the ScreenManager.

class kivy.uix.screenmanager.FadeTransition[source]

Bases: kivy.uix.screenmanager.ShaderTransition

Fade transition, based on a fragment Shader.

class kivy.uix.screenmanager.FallOutTransition[source]

Bases: kivy.uix.screenmanager.ShaderTransition

Transition where the new screen ‘falls’ from the screen centre, becoming smaller and more transparent until it disappears, and revealing the new screen behind it. Mimics the popular/standard Android transition.

New in version 1.8.0.

duration

Duration in seconds of the transition, replacing the default of TransitionBase.

duration is a NumericProperty and defaults to .15 (= 150ms).

class kivy.uix.screenmanager.NoTransition[source]

Bases: kivy.uix.screenmanager.TransitionBase

No transition, instantly switches to the next screen with no delay or animation.

New in version 1.8.0.

class kivy.uix.screenmanager.RiseInTransition[source]

Bases: kivy.uix.screenmanager.ShaderTransition

Transition where the new screen rises from the screen centre, becoming larger and changing from transparent to opaque until it fills the screen. Mimics the popular/standard Android transition.

New in version 1.8.0.

duration

Duration in seconds of the transition, replacing the default of TransitionBase.

duration is a NumericProperty and defaults to .2 (= 200ms).

class kivy.uix.screenmanager.Screen(**kw)[source]

Bases: kivy.uix.relativelayout.RelativeLayout

Screen is an element intended to be used with a ScreenManager. Check module documentation for more information.

Events
on_pre_enter: ()

Event fired when the screen is about to be used: the entering animation is started.

on_enter: ()

Event fired when the screen is displayed: the entering animation is complete.

on_pre_leave: ()

Event fired when the screen is about to be removed: the leaving animation is started.

on_leave: ()

Event fired when the screen is removed: the leaving animation is finished.

Changed in version 1.6.0: Events on_pre_enter, on_enter, on_pre_leave and on_leave were added.

manager

ScreenManager object, set when the screen is added to a manager.

manager is an ObjectProperty and defaults to None, read-only.

name

Name of the screen which must be unique within a ScreenManager. This is the name used for ScreenManager.current.

name is a StringProperty and defaults to ‘’.

transition_progress

Value that represents the completion of the current transition, if any is occurring.

If a transition is in progress, whatever the mode, the value will change from 0 to 1. If you want to know if it’s an entering or leaving animation, check the transition_state.

transition_progress is a NumericProperty and defaults to 0.

transition_state

Value that represents the state of the transition:

  • ‘in’ if the transition is going to show your screen

  • ‘out’ if the transition is going to hide your screen

After the transition is complete, the state will retain its last value (in or out).

transition_state is an OptionProperty and defaults to ‘out’.

class kivy.uix.screenmanager.ScreenManager(**kwargs)[source]

Bases: kivy.uix.floatlayout.FloatLayout

Screen manager. This is the main class that will control your Screen stack and memory.

By default, the manager will show only one screen at a time.

add_widget(screen)[source]

Add a new widget as a child of this widget.

Parameters
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.

New in version 1.0.5.

canvas: str, defaults to None

Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.

New in version 1.9.0.

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
clear_widgets(screens=None)[source]

Remove all (or the specified) children of this widget. If the ‘children’ argument is specified, it should be a list (or filtered list) of children of the current widget.

Changed in version 1.8.0: The children argument can be used to specify the children you want to remove.

current

Name of the screen currently shown, or the screen to show.

from kivy.uix.screenmanager import ScreenManager, Screen

sm = ScreenManager()
sm.add_widget(Screen(name='first'))
sm.add_widget(Screen(name='second'))

# By default, the first added screen will be shown. If you want to
# show another one, just set the 'current' property.
sm.current = 'second'

current is a StringProperty and defaults to None.

current_screen

Contains the currently displayed screen. You must not change this property manually, use current instead.

current_screen is an ObjectProperty and defaults to None, read-only.

get_screen(name)[source]

Return the screen widget associated with the name or raise a ScreenManagerException if not found.

has_screen(name)[source]

Return True if a screen with the name has been found.

New in version 1.6.0.

next()[source]

Return the name of the next screen from the screen list.

on_touch_down(touch)[source]

Receive a touch down event.

Parameters
touch: MotionEvent class

Touch received. The touch is in parent coordinates. See relativelayout for a discussion on coordinate systems.

Returns

bool If True, the dispatching of the touch event will stop. If False, the event will continue to be dispatched to the rest of the widget tree.

on_touch_move(touch)[source]

Receive a touch move event. The touch is in parent coordinates.

See on_touch_down() for more information.

on_touch_up(touch)[source]

Receive a touch up event. The touch is in parent coordinates.

See on_touch_down() for more information.

previous()[source]

Return the name of the previous screen from the screen list.

remove_widget(*l)[source]

Remove a widget from the children of this widget.

Parameters
widget: Widget

Widget to remove from our children list.

>>> from kivy.uix.button import Button
>>> root = Widget()
>>> button = Button()
>>> root.add_widget(button)
>>> root.remove_widget(button)
screen_names

List of the names of all the Screen widgets added. The list is read only.

screens_names is an AliasProperty and is read-only. It is updated if the screen list changes or the name of a screen changes.

screens

List of all the Screen widgets added. You should not change this list manually. Use the add_widget method instead.

screens is a ListProperty and defaults to [], read-only.

switch_to(screen, **options)[source]

Add a new or existing screen to the ScreenManager and switch to it. The previous screen will be “switched away” from. options are the transition options that will be changed before the animation happens.

If no previous screens are available, the screen will be used as the main one:

sm = ScreenManager()
sm.switch_to(screen1)
# later
sm.switch_to(screen2, direction='left')
# later
sm.switch_to(screen3, direction='right', duration=1.)

If any animation is in progress, it will be stopped and replaced by this one: you should avoid this because the animation will just look weird. Use either switch_to() or current but not both.

The screen name will be changed if there is any conflict with the current screen.

transition

Transition object to use for animating the transition from the current screen to the next one being shown.

For example, if you want to use a WipeTransition between slides:

from kivy.uix.screenmanager import ScreenManager, Screen,
WipeTransition

sm = ScreenManager(transition=WipeTransition())
sm.add_widget(Screen(name='first'))
sm.add_widget(Screen(name='second'))

# by default, the first added screen will be shown. If you want to
# show another one, just set the 'current' property.
sm.current = 'second'

transition is an ObjectProperty and defaults to a SlideTransition.

Changed in version 1.8.0: Default transition has been changed from SwapTransition to SlideTransition.

exception kivy.uix.screenmanager.ScreenManagerException[source]

Bases: Exception

Exception for the ScreenManager.

class kivy.uix.screenmanager.ShaderTransition[source]

Bases: kivy.uix.screenmanager.TransitionBase

Transition class that uses a Shader for animating the transition between 2 screens. By default, this class doesn’t assign any fragment/vertex shader. If you want to create your own fragment shader for the transition, you need to declare the header yourself and include the “t”, “tex_in” and “tex_out” uniform:

# Create your own transition. This shader implements a "fading"
# transition.
fs = """$HEADER
    uniform float t;
    uniform sampler2D tex_in;
    uniform sampler2D tex_out;

    void main(void) {
        vec4 cin = texture2D(tex_in, tex_coord0);
        vec4 cout = texture2D(tex_out, tex_coord0);
        gl_FragColor = mix(cout, cin, t);
    }
"""

# And create your transition
tr = ShaderTransition(fs=fs)
sm = ScreenManager(transition=tr)
add_screen(screen)[source]

(internal) Used to add a screen to the ScreenManager.

clearcolor

Sets the color of Fbo ClearColor.

New in version 1.9.0.

clearcolor is a ColorProperty and defaults to [0, 0, 0, 1].

Changed in version 2.0.0: Changed from ListProperty to ColorProperty.

fs

Fragment shader to use.

fs is a StringProperty and defaults to None.

remove_screen(screen)[source]

(internal) Used to remove a screen from the ScreenManager.

stop()[source]

(internal) Stops the transition. This is automatically called by the ScreenManager.

vs

Vertex shader to use.

vs is a StringProperty and defaults to None.

class kivy.uix.screenmanager.SlideTransition[source]

Bases: kivy.uix.screenmanager.TransitionBase

Slide Transition, can be used to show a new screen from any direction: left, right, up or down.

direction

Direction of the transition.

direction is an OptionProperty and defaults to ‘left’. Can be one of ‘left’, ‘right’, ‘up’ or ‘down’.

class kivy.uix.screenmanager.SwapTransition(**kwargs)[source]

Bases: kivy.uix.screenmanager.TransitionBase

Swap transition that looks like iOS transition when a new window appears on the screen.

add_screen(screen)[source]

(internal) Used to add a screen to the ScreenManager.

start(manager)[source]

(internal) Starts the transition. This is automatically called by the ScreenManager.

class kivy.uix.screenmanager.TransitionBase[source]

Bases: kivy.event.EventDispatcher

TransitionBase is used to animate 2 screens within the ScreenManager. This class acts as a base for other implementations like the SlideTransition and SwapTransition.

Events
on_progress: Transition object, progression float

Fired during the animation of the transition.

on_complete: Transition object

Fired when the transition is finished.

add_screen(screen)[source]

(internal) Used to add a screen to the ScreenManager.

duration

Duration in seconds of the transition.

duration is a NumericProperty and defaults to .4 (= 400ms).

Changed in version 1.8.0: Default duration has been changed from 700ms to 400ms.

is_active

Indicate whether the transition is currently active or not.

is_active is a BooleanProperty and defaults to False, read-only.

manager

ScreenManager object, set when the screen is added to a manager.

manager is an ObjectProperty and defaults to None, read-only.

remove_screen(screen)[source]

(internal) Used to remove a screen from the ScreenManager.

screen_in

Property that contains the screen to show. Automatically set by the ScreenManager.

screen_in is an ObjectProperty and defaults to None.

screen_out

Property that contains the screen to hide. Automatically set by the ScreenManager.

screen_out is an ObjectProperty and defaults to None.

start(manager)[source]

(internal) Starts the transition. This is automatically called by the ScreenManager.

stop()[source]

(internal) Stops the transition. This is automatically called by the ScreenManager.

class kivy.uix.screenmanager.WipeTransition[source]

Bases: kivy.uix.screenmanager.ShaderTransition

Wipe transition, based on a fragment Shader.