pythonkivy

How do I implement a Splash screen for my app?


I have a simple test application, and I want to see how to add a splash screen to my app. I tried using a popup for this (is there a better way?), and the popup will preferably have no buttons, just an image that lingers for a short amount of time, then the popup closes automatically.

What I have now seems to do nothing. Here's my Python code:

import kivy
from kivy.app import App

# below this kivy version you cannot use the app
kivy.require('1.9.0')

from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout


class LoadSplashScreenDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


class LayoutTestApp(App):
    SplashImage = 'images/GrapeVineSplashText.jpg'

    def __init__(self, **kwargs):
        super().__init__()
        content = LoadSplashScreenDialog(cancel=self.dismiss_popup)
        self._popup = Popup(title="Test App", content=content, size_hint=(1.0, 1.0))
        self._popup.open()
        # self._popup.dismiss()

    def dismiss_popup(self):
        self._popup.dismiss()
    ...

I have a call to _popup.dismiss() after _popup.open(), but it is currently commented out, because I don't (yet) know how to wait for a specified amount of time before automatically closing the popup. So, at this point, I just want to see that the splash screen appears.

Here is my kv code:

<LoadSplashScreenDialog>:
    BoxLayout:
        orientation: "vertical"
        size: root.size
        pos: root.pos
        Image:
            id: image
            source: app.SplashImage
            fit_mode: "contain"     # resized to fit, maintaining aspect ratio
            padding: 2
            size_hint_x: 1.0
            canvas:
                Color:
                    rgba: 0.1, 0.1, 0.1, 1  # ? gray color
                Line:
                    width: 2
                    rectangle: self.x, self.y, self.width, self.height

Am I close? Am I way off? How would I get this to work???


Solution

  • I found a solution for adding a splash screen. It suddenly occurred to me that having the popup code in my app's __init__() method could be the cause of the problem, because the app might not be finished initializing and therefore not ready to display a popup.

    Moving the popup code to my app's build() method also did not work, probably for similar reasons.

    So I coded the following:

        def __init__(self, **kwargs):
            super().__init__()
            self._popup = None
    
        def build(self):
            # show splash screen immediately after build() completes
            Clock.schedule_once(self.ShowSplashScreen, 0)
            return self.mainLayout
    
        def ShowSplashScreen(self, *args):
            content = LoadSplashScreenDialog(cancel=self.dismiss_popup)
            self._popup = Popup(title="Test App", content=content, size_hint=(1.0, 1.0))
            self._popup.open()
            Clock.schedule_once(self.dismiss_popup, 2)
    

    By scheduling the popup to display in the next frame, it will display immediately after the build() method completes and the app is instantiated.