简体   繁体   English

App.root 必须是 Widget 的_实例_。 App.root 中的实例无效

[英]App.root must be an _instance_ of Widget. Invalid instance in App.root

I'm not exactly new to python but wouldn't I know it well and fully, but I'm completely new to Kivy. It's just a small thing I'm doing just to start somewhere.我不是 python 的新手,但我不是很了解它吗,但我是 Kivy 的新手。这只是我做的一件小事,只是为了从某个地方开始。 I'm working in Visual Studio Code(VSC).我在 Visual Studio 代码 (VSC) 中工作。 Python version 3.10. Python 版本 3.10。 The idea is: there are 5 buttons, each button has a name, when you click on a button, image(meme) should show up.这个想法是:有 5 个按钮,每个按钮都有一个名称,当你点击一个按钮时,图像(meme)应该出现。 Here is the full code, terminal, debugger and what is in command prompt:这是完整的代码、终端、调试器以及命令提示符中的内容:

code code代码代码

Terminal Terminal终端终端

Debugger Debugger调试器调试器

Command Prompt Command Prompt命令提示符命令提示符

Here is python itself and a folder "Projects" C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10 enter image description here这是 python 本身和文件夹“Projects” C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10在此处输入图像描述

Here is folder with virtual environment(App_EPQ) and a folder with code(App_code) C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Projects enter image description here这是带有虚拟环境 (App_EPQ) 的文件夹和带有代码 (App_code) 的文件夹C:\Users\Кирилл\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Projects enter image description here

Thanks in advance提前致谢

Tried to rewrite the code but that didn't work.试图重写代码,但没有奏效。 Tried to find someone with a similar problem no luck there also.试图找到有类似问题的人也没有运气。 No idea what to do next and how to fix it so this is my last hope.不知道下一步该做什么以及如何解决它,所以这是我最后的希望。

Your class has to inherit a layout as well as App:您的 class 必须继承布局和 App:

    class HBoxLayoutExample(BoxLayout, App)

Another way to do it would be to create a class that builds the app and returns the layout:另一种方法是创建一个 class 来构建应用程序并返回布局:

    class HBoxLayoutExample(BoxLayout)
    **your code**

    class HBoxApp(App):
        def build(self):
            return HBoxLayoutExample
    if __name__ == '__main__':
        HBoxApp().run()

as shown here https://kivy.org/doc/stable/guide/lang.html#designing-with-the-kivy-language如此处所示https://kivy.org/doc/stable/guide/lang.html#designing-with-the-kivy-language

Replace your line:替换你的行:

return layout, button

with:和:

return layout

That's all.就这样。 build() method have to return one widget, which in your case is BoxLayout instance. build()方法必须返回一个小部件,在您的情况下是 BoxLayout 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM