【问题标题】:kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widgetkivy.uix.screenmanager.ScreenManagerException:ScreenManager 只接受 Screen 小部件
【发布时间】:2020-02-16 03:48:36
【问题描述】:

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

这里是代码。

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.

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

【问题讨论】:

  • 我了解您想要对第一个屏幕执行的操作:您希望图像在显示该屏幕时从不透明度 0 变为 1。但是我不明白第二个屏幕的目的是什么
  • 我不能为两个动画使用同一个屏幕@eyllanesc
  • 我想你很困惑,一个屏幕可以有很多动画。

标签: python python-3.x kivy


【解决方案1】:

您的代码存在以下问题:

  • ScreenManager 需要 Screen 类型对象,不等待 Screen 类。
  • 必须为每个屏幕设置一个名称,并且该名称必须传递给当前屏幕。
  • 图像必须通过布局放置在屏幕内。
  • 动画必须在屏幕显示时执行的on_enter方法中启动。
  • App 的 build 方法必须返回一个小部件。
  • 如果您希望连续播放 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()

【讨论】:

  • 我需要我的 opencity ico 慢慢出现,然后慢慢隐藏@eyllanesc
猜你喜欢
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
相关资源
最近更新 更多