kivy.uix.screenmanager.ScreenManagerException:ScreenManager 只接受 Screen 小部件
问题描述
我正在尝试创建启动画面.
I am trying to create a splash screen.
这里是代码.
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 WorksKivy Testopencityicon.png", pos=(0, 0), opacity=0)
anim1 = Animation(duration=4, opacity=1)
anim1.start(wing1)
class ScreenTwo(Screen):
wing = Image(source="F:PyCharm Python WorksKivy Testopencityicon.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()
错误:
[INFO ] [Logger ] Record log in C:Userskanna.kivylogskivy_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 Kivylibsite-packageskivy\__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 KivyScriptspython.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 Kivylibsite-packageskivyuixscreenmanager.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.
解决方案
你的代码有以下问题:
- ScreenManager 需要 Screen 类型的对象,不等待 Screen 类.
- 必须为每个屏幕设置一个名称,并且该名称必须传递给当前屏幕.
- 图片必须通过布局放置在屏幕内.
- 动画必须在屏幕显示时执行的on_enter方法中启动.
- App 的 build 方法必须返回一个 widget.
- 如果您想要连续播放 2 个动画,则必须使用+"运算符.
考虑到上面的解决方案是:
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()
相关文章