Python | ScreenManager in Kivy using .kv file

16 Jan, 2022
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. 
The screen manager is a widget which is used 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.The ScreenManager and Screen class are imported. The ScreenManager will be used for the root like: 
from kivy.uix.screenmanager import ScreenManager, Screen
Note:The default ScreenManager.transition is a SlideTransition with options direction and duration. 
Basic Approach:
1) import kivy
2) import kivyApp
3) import Screen Manager, Screen, ""Transitions you want to use"" 
4) Set minimum version(optional)
5) Create Different Screen classes and pass them
6) Create features of Screen classes in .kv file 
       :: Add features in different screens
7) Create App class
8) return screen manager
9) Run an instance of the class
Below is the implementation of the code with .kv file in .py file. 
Python3
# Program to Show how to create a switch# import kivy module   import kivy       # base Class of your App inherits from the App class.   # app:always refers to the instance of your application  from kivy.app import App    # this restrict the kivy version i.e # below this kivy version you cannot # use the app or software kivy.require('1.9.0') # Builder is used when .kv file is# to be used in .py filefrom kivy.lang import Builder # The screen manager is a widget# dedicated to managing multiple screens for your application.from kivy.uix.screenmanager import ScreenManager, Screen  # You can create your kv code in the Python fileBuilder.load_string("""<ScreenOne>:    BoxLayout:        Button:            text: "Go to Screen 2"            background_color : 0, 0, 1, 1            on_press:                # You can define the duration of the change                # and the direction of the slide                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 screenclass ScreenOne(Screen):    pass  class ScreenTwo(Screen):    pass class ScreenThree(Screen):    pass class ScreenFour(Screen):    pass class ScreenFive(Screen):    pass    # The ScreenManager controls moving between screensscreen_manager = ScreenManager()  # Add the screens to the manager and then supply a name# that is used to switch screensscreen_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")) # Create the App classclass ScreenApp(App):    def build(self):        return screen_manager # run the appsample_app = ScreenApp()sample_app.run()
Output:  
 
 
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 center while fading from transparent to opaque.
You can easily switch transitions by changing the ScreenManager.transition property:  
sm = ScreenManager(transition=FadeTransition())
 
Python3
# Program to Show how to create a switch# import kivy module   import kivy       # base Class of your App inherits from the App class.   # app:always refers to the instance of your application  from kivy.app import App    # this restrict the kivy version i.e # below this kivy version you cannot # use the app or software kivy.require('1.9.0') # Builder is used when .kv file is# to be used in .py filefrom kivy.lang import Builder # The screen manager is a widget# dedicated to managing multiple screens for your application.from kivy.uix.screenmanager import (ScreenManager, Screen, NoTransition,SlideTransition, CardTransition, SwapTransition,FadeTransition, WipeTransition, FallOutTransition, RiseInTransition)  # You can create your kv code in the Python fileBuilder.load_string("""<ScreenOne>:    BoxLayout:        Button:            text: "Go to Screen 2"            background_color : 0, 0, 1, 1            on_press:                # You can define the duration of the change                # and the direction of the slide                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 screenclass ScreenOne(Screen):    pass  class ScreenTwo(Screen):    pass class ScreenThree(Screen):    pass class ScreenFour(Screen):    pass class ScreenFive(Screen):    pass    # The ScreenManager controls moving between screens# You can change the transitions accordinglyscreen_manager = ScreenManager(transition = RiseInTransition())  # Add the screens to the manager and then supply a name# that is used to switch screensscreen_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")) # Create the App classclass ScreenApp(App):    def build(self):        return screen_manager # run the appsample_app = ScreenApp()sample_app.run()
Note: Code is Same some points is added in code don’t confuse.Output Video of Different transitions –  
 
ruhelaa48
saurabh1990aror
Python-gui
Python-kivy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.