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

标签 python python-3.x kivy

我正在尝试创建启动画面。

这是代码。

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()

错误:

[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.

这是错误日志。为什么会显示这样。我尝试过编辑,但根本不起作用。 因此,鉴于代码在编辑时不起作用。 提前致谢。 我尝试仅使用动画。但它也不起作用。

最佳答案

您的代码存在以下问题:

  • ScreenManager 需要 Screen 类型对象,而不是等待 Screen 类。
  • 必须为每个屏幕设置名称,并且该名称必须传递给当前屏幕。
  • 图像必须通过布局放置在屏幕内。
  • 动画必须在屏幕显示时执行的 on_enter 方法中启动。
  • 应用程序的构建方法必须返回一个小部件。
  • 如果您想要连续播放 2 个动画,则必须使用“+”运算符。

考虑到上述情况,解决方案是:

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()

关于python - kivy.uix.screenmanager.ScreenManagerException : ScreenManager accepts only Screen widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60245242/

相关文章:

python - 就地矩阵乘法 numpy

python - 将条件表达式用于副作用是否有效?

python - Kivy TextInput 自动完成。如何使用 kv 文件获得相同的结果?

Django Postgres 不区分大小写的文本字段和 citext

KV lang 文件中的 SVG

python - 如何打包适用于iOS的Kivy应用程序?

python - 概念化分解枚举的列表理解

python - 为什么控制台卡在我用 python 解决 9X9 数独板的最后一个函数上?

python - 同时使用组和个人权限

python - 自定义 Django 字段不从查询返回 Enum 实例