程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

我有一个错误,如:ScreenManager accepts only Screen widget in simple code

发布于2023-03-20 23:17     阅读(1089)     评论(0)     点赞(2)     收藏(4)


我的问题不是很复杂,因为我最近开始与 kivy 接口并通过在教程中运行代码(代码工作的地方)它会生成这种类型的错误,在互联网上和教程本身中,我不会找到了解决办法。

main.py:

from kivy.app import App


class MainApp(App):
    pass


MainApp().run()

主要.kv:

#:include Screen1.kv
#:include Screen2.kv

ScreenManager:
    Screen:
        name: "main"
    BoxLayout:
        Button:
            text: "go to Screen1"
            on_release: app.root.current = "screen1"
        Button:
            text: "go to Screen2"
            on_release: app.root.current = "screen2"

    Screen1:
    Screen2:

屏幕 1.kv:

<Screen1@Screen>:
    name: "screen1"
    BoxLayout:
        Button:
            text: "go to main"
            on_release: app.root.current = "main"

屏幕2.kv:

<Screen2@Screen>:
    name: "screen2"
    BoxLayout:
        Button:
            text: "go to main"
            on_release: app.root.current = "main"

错误是这样的:

C:\Users\39377\AppData\Local\Microsoft\WindowsApps\python3.10.exe C:\Users\39377\Desktop\APP\BubiniProject\BubiniApp\main.py 
[INFO   ] [Logger      ] Record log in C:\Users\39377\.kivy\logs\kivy_23-03-19_33.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.3
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.4.5
[INFO   ] [Kivy        ] v2.1.0
[INFO   ] [Kivy        ] Installed at "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.10.10 (tags/v3.10.10:aad5f6a, Feb  7 2023, 17:20:36) [MSC v.1929 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\39377\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 189 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, 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.7870'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) UHD Graphics 620'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 26.20.100.7870'>
[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
 Traceback (most recent call last):
   File "C:\Users\39377\Desktop\APP\BubiniProject\BubiniApp\main.py", line 8, in <module>
     MainApp().run()
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 954, in run
     self._run_prepare()
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 923, in _run_prepare
     self.load_kv(filename=self.kv_file)
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\app.py", line 696, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 305, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 407, in load_string
     self._apply_rule(
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\lang\builder.py", line 659, in _apply_rule
     widget.add_widget(child)
   File "C:\Users\39377\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\kivy\uix\screenmanager.py", line 987, in add_widget
     raise ScreenManagerException(
 kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.

Process finished with exit code 1

我不明白为什么使用这样一个简单的程序并编写从教程中复制的代码(在视频中可以清楚地看到代码已成功执行)会生成此错误。提前致谢。


解决方案


在您的 中kv,您试图将 a 添加BoxLayoutScreenManager,这会导致该错误。也许您打算将BoxLayout添加到Screen? 尝试改变:

ScreenManager:
    Screen:
        name: "main"
    BoxLayout:
        Button:
            text: "go to Screen1"
            on_release: app.root.current = "screen1"
        Button:
            text: "go to Screen2"
            on_release: app.root.current = "screen2"

    Screen1:
    Screen2:

到:

ScreenManager:
    Screen:
        name: "main"
        BoxLayout:
            Button:
                text: "go to Screen1"
                on_release: app.root.current = "screen1"
            Button:
                text: "go to Screen2"
                on_release: app.root.current = "screen2"

    Screen1:
    Screen2:

注意 的缩进BoxLayout



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1940532/0c9573a1e2dc3d767d92/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)