Sarthak Mahapatra
Sarthak Mahapatra

Reputation: 133

How to create a Splash Screen using kivymd

I am new to kivymd and I am trying to create a splash screen using kivymd. But all the tutorials show screen-transition after clicking a button. What I want is a modular code in .kv file so that first the WindowManager will open the Splash screen, wait for 5sec and then open the home screen of the App. I tried the following code but I am unable to understand how do I make use of the Clock class to open the Splash Screen first, then wait for 5sec and finally move to the home screen? PS - I saw this question in StackOverflow but this isn't solving my purpose How to add a Splash Screen in KivyMD

Main.py

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock

import time

main_kv = Builder.load_file(r'.\kv\main.kv')
Window.size = (400, 700)


class SplashScreen():
    """This class will show the splash screen of Docto365"""


class HomeScreen():
    """This class will show the Home screen of Doctor365"""


class WindowManager(ScreenManager):
    """This class will handle the screen transitions"""


class Test(MDApp):
    def build(self):
        self.theme_cls.primary_palette = 'Indigo'
        self.theme_cls.accent_palette = 'Blue'
        self.theme_cls.theme_style = 'Light'
        return main_kv


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

main.kv

<WindowManager>:
    SplashScreen:
    HomeScreen

<SplashScreen>:
    MDLabel:
        text: "Splash Screen"

<HomeScreen>:
    MDLabel:
        text: "Home Screen"

Upvotes: 0

Views: 2911

Answers (2)

watney
watney

Reputation: 141

Here are a few flaws with your current code:

  • Since you're loading the kivy code and returning it inside your app's build method, you need to have a root widget inside the kivy code.
  • Your custom screen classes must be inherited from Kivy's Screen class (kivy.uix.screenmanager.Screen).
  • Make sure you load your kivy code after all class declarations because your kivy code needs those classes and hence they'd raise an error if the required class is not found in your python code.

Now to your question, since you want to change the screen automatically after a fixed amount of time you can use Clock.schedule_once.

Updated code:

Kivy - Make WindowManager as root and give names to screens

WindowManager:
    SplashScreen:
        name: "splash"
    HomeScreen:
        name: "home"

<SplashScreen>:
    MDLabel:
        text: "Splash Screen"

<HomeScreen>:
    MDLabel:
        text: "Home Screen"

python

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.lang.builder import Builder
from kivy.clock import Clock

import time

#main_kv = Builder.load_string(kv)
Window.size = (400, 700)

class SplashScreen(Screen):
    """This class will show the splash screen of Docto365"""


class HomeScreen(Screen):
    """This class will show the Home screen of Doctor365"""


class WindowManager(ScreenManager):
    """This class will handle the screen transitions"""


class Test(MDApp):
    def build(self):
        self.theme_cls.primary_palette = 'Indigo'
        self.theme_cls.accent_palette = 'Blue'
        self.theme_cls.theme_style = 'Light'
        
        self.window_manager = Builder.load_string(kv)
        
        Clock.schedule_once(self.go_to_home, 5)
        
        return self.window_manager
    
    def go_to_home(self, *args):
        self.window_manager.current = 'home'


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

Note: You can use a different screen transition to look it more like fading from splash to home

PS: I'd recommend you not use this functionality for kivy mobile apps because this will just add additional waiting time for your users in addition to default splash time.

Upvotes: 0

John Anderson
John Anderson

Reputation: 38937

You can use the on_enter() method of Screen, like this:

class SplashScreen(Screen):
    """This class will show the splash screen of Docto365"""
    def on_enter(self, *args):
        Clock.schedule_once(self.switch_to_home, 5)

    def switch_to_home(self, dt):
        self.manager.current = 'Home'

along with some required changes to your .kv:

WindowManager:  # eliminate "<>" to make this a root widget
    SplashScreen:
    HomeScreen

<SplashScreen>:
    name: 'Splash'  # name is required
    MDLabel:
        text: "Splash Screen"

<HomeScreen>:
    name: 'Home'  # name is required
    MDLabel:
        text: "Home Screen"

And move your call to builder.load_file() inside the build() method:

class Test(MDApp):
    def build(self):
        main_kv = Builder.load_file(r'.\kv\main.kv')
        self.theme_cls.primary_palette = 'Indigo'
        self.theme_cls.accent_palette = 'Blue'
        self.theme_cls.theme_style = 'Light'
        return main_kv

Upvotes: 1

Related Questions