Sairam Mangeshkar
Sairam Mangeshkar

Reputation: 371

kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget

I am trying to create a splash screen.

Here are the codes.

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.animation import Animation




class ScreenOne(Screen):

        wing1 = Image(source="F:\PyCharm Python Works\Kivy Test\opencityicon.png", pos=(0, 0), opacity=0)
        anim1 = Animation(duration=4, opacity=1)
        anim1.start(wing1)


class ScreenTwo(Screen):

        wing = Image(source="F:\PyCharm Python Works\Kivy Test\opencityicon.png", pos=(0, 0), opacity=1)
        animation = Animation(duration=2, opacity=0)
        animation.start(wing)


sm = ScreenManager()
sm.add_widget(ScreenOne)
sm.add_widget(ScreenTwo)
sm.current = ScreenOne


class Arge(App):

    def build(self):
        pass



if __name__ == "__main__":
    Arge().run()

error:

[INFO   ] [Logger      ] Record log in C:\Users\kanna\.kivy\logs\kivy_20-02-16_16.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.2.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.1.23
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "F:\Python Kivy\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.7.6 (tags/v3.7.6:43364a7ae0, Dec 18 2019, 23:46:00) [MSC v.1916 32 bit (Intel)]
[INFO   ] [Python      ] Interpreter at "F:\Python Kivy\Scripts\python.exe"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 26.20.100.7262'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) UHD Graphics 630'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 26.20.100.7262'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
 Traceback (most recent call last):
   File "F:/PyCharm Python Works/Kivy Test/splashscreen.py", line 25, in <module>
     sm.add_widget(ScreenOne)
   File "F:\Python Kivy\lib\site-packages\kivy\uix\screenmanager.py", line 979, in add_widget
     'ScreenManager accepts only Screen widget.')
 kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.

This is the error log. Why does it shows like this. I tried editing but it doesn't work at all. So given the code that it doesn't working when edited. Thanks in advance. I tried using the Animation only. but it doesn't work also.

Upvotes: 0

Views: 3892

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

Your code has the following problems:

  • ScreenManager expects Screen type objects, does not wait for the Screen class.
  • A name must be set for each screen and that name must be passed to current.
  • The Image must be placed inside the screen through a layout.
  • The animation must be started in the on_enter method that is executed when the screen is displayed.
  • The build method of the App must return a widget.
  • If you want 2 animations sequentially then you must use the "+" operator.

Considering the above the solution is:

import os

from kivy.app import App

from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.animation import Animation

current_dir = os.path.dirname(os.path.realpath(__file__))


class ScreenOne(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.img = Image(source=os.path.join(current_dir, "opencityicon.png"))
        box_layout = BoxLayout()
        self.add_widget(box_layout)
        box_layout.add_widget(self.img)

    def on_enter(self):
        self.img.opacity = 0
        animation = Animation(duration=4, opacity=1) + Animation(duration=4, opacity=0)
        animation.start(self.img)


sm = ScreenManager()
sm.add_widget(ScreenOne(name="screen_one"))
sm.current = "screen_one"


class Arge(App):
    def build(self):
        return sm


if __name__ == "__main__":
    Arge().run()

Upvotes: 3

Related Questions