简体   繁体   English

Kivy 错误:raise FactoryException('Unknown class <%s>' % name)

[英]Kivy error: raise FactoryException('Unknown class <%s>' % name)

I have searched and searched, and i just don´t get it.我已经搜索并搜索了,但我就是不明白。 From what i understand from posted answers, the problem is the class is not defined or spelled badly, but i have gone back and forth through my code and i can´t see the problem.从我从发布的答案中了解到,问题是该类没有定义或拼写错误,但我已经来回浏览了我的代码,但我看不到问题。 Right now i´m just trying to get the layout there is no funcionality.现在我只是想获得没有功能的布局。 I have two files, the .py and the .kv file, the main .py is:我有两个文件,.py 和 .kv 文件,主要的 .py 是:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class Noughtsandcrosses(Widget):
    pass


class nandxApp(App):
    def build(self):
        return Noughtsandcrosses()

if __name__ == "__main__":
    nandxApp().run()

and the .kv file is: .kv 文件是:

#:kivy 1.0

<Noughtsandcrosses>:
    orientation: 'vertical'
    size: self.size

    Threebythreeone:
        orientation: 'horizontal'

        Button:
            Image:
                source: "blank.png"
                size: 100, 100

running the .py file this is the error i get:运行 .py 文件,这是我得到的错误:

Traceback (most recent call last):
   File "nandx.py", line 24, in <module>
     nandxApp().run()
   File "C:\Users\Rayne\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "nandx.py", line 21, in build
     return Noughtsandcrosses()
   File "C:\Users\Rayne\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 345, in __init__
     Builder.apply(self, ignored_consts=self._kwargs_applied_init)
   File "C:\Users\Rayne\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 451, in apply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "C:\Users\Rayne\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 526, in _apply_rule
     cls = Factory_get(cname)
   File "C:\Users\Rayne\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\factory.py", line 131, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <Threebythreeone>

i´m trying hard to learn kivy but it´s so frustrating getting a random error like this, can anyone point out what i´ve done wrong please.我正在努力学习 kivy,但遇到这样的随机错误令人沮丧,谁能指出我做错了什么。

You encountered the error, kivy.factory.FactoryException: Unknown class <Threebythreeone> because you have a child widget Threebythreeone in the <Noughtsandcrosses> widget rule, nandx.kv您遇到错误 kivy.factory.FactoryException: Unknown class <Threebythreeone> 因为您在<Noughtsandcrosses>小部件规则nandx.kv中有一个子小部件 Threebythreeone

I recommend that you check out Programming Guide » Kivy Basics and the example below.我建议您查看编程指南 » Kivy 基础知识和下面的示例。

Example示例

main.py主文件

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class Threebythreeone(BoxLayout):
    pass


class Noughtsandcrosses(BoxLayout):
    pass


class nandxApp(App):
    def build(self):
        return Noughtsandcrosses()


if __name__ == "__main__":
    nandxApp().run()

nandx.kv nandx.kv

#:kivy 1.10.0

<Threebythreeone>:
    # orientation: 'horizontal'   # Commented off because this is the default

    Button:
        Image:
            source: "blank.png"
            size: 100, 100

<Noughtsandcrosses>:
    orientation: 'vertical'
    size: self.size

    Threebythreeone:

Output输出

在此处输入图片说明

You are getting this error because you haven't declared the Threebythreeone before using it.您收到此错误是因为您在使用它之前没有声明Threebythreeone You can declare it in python like you have with Noughtsandcrosses or declare it in your kv file as in my example below.您可以像使用Noughtsandcrosses一样在 python 中声明它,也可以在kv文件中声明它,如下面的示例所示。

See docs for more info about kv language.有关kv语言的更多信息,请参阅文档

Also, Noughtsandcrosses doesn't have a property orientation as far as I know.此外,据我所知, Noughtsandcrosses没有财产orientation You should probably use BoxLayout .您可能应该使用BoxLayout

And, I don't think how you built your button is going to work as you expect.而且,我认为您构建按钮的方式不会像您期望的那样工作。 See this and this for some information.有关一些信息,请参阅

Below will work:下面将工作:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder

Builder.load_string("""
# This is all that is necessary to declare a class based on BoxLayout.
<Threebythreeone@BoxLayout>:

<Noughtsandcrosses>:
    orientation: 'vertical'
    size: self.size

    Threebythreeone:
        orientation: 'horizontal'

        Button:
            Image:
                source: "blank.png"
                size: 100, 100
""")


class Noughtsandcrosses(BoxLayout):
    pass


class nandxApp(App):
    def build(self):
        return Noughtsandcrosses()


if __name__ == "__main__":
    nandxApp().run()

just remove the只需删除
orientation: 'horizontal'方向:'水平'

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

相关问题 kivy 错误 - kivy.factory.FactoryException:未知类<WindowManager> - kivy error - kivy.factory.FactoryException: Unknown class <WindowManager> kivy.factory.FactoryException:未知 class<windowmanager></windowmanager> - kivy.factory.FactoryException: Unknown class <WindowManager> kivy.factory.FactoryException:未知 class<mdtopappbar></mdtopappbar> - kivy.factory.FactoryException: Unknown class <MDTopAppBar> 尝试在Kivy中创建“ IconButton”时出现FactoryException - FactoryException when trying to create a “IconButton” in Kivy Kivy 未知类<NavigationDrawerIconButton> - Kivy Unknown class <NavigationDrawerIconButton> 如果子类在python中覆盖父类的方法,如何引发错误? - How to raise an error if child class override parent's method in python? 如何修复:引发 TypeError(“将 %s 转换为 TensorShape 时出错:%s。” % (arg_name, e))? - How to fix : raise TypeError(“Error converting %s to a TensorShape: %s.” % (arg_name, e))? 问:Kivy 无效的类名 - Q: Kivy Invalid Class Name raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException:消息:未知错误:找不到 Chrome 二进制文件 - raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary Class inheritance 与 Kivy 的 RecycleView - Class inheritance with Kivy's RecycleView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM