【问题标题】:Kivy error: raise FactoryException('Unknown class <%s>' % name)Kivy 错误:引发 FactoryException('Unknown class <%s>' % name)
【发布时间】:2018-02-17 21:28:27
【问题描述】:

我已经搜索和搜索,但我就是不明白。根据我从发布的答案中了解到的情况,问题是该类没有定义或拼写错误,但我在我的代码中来回走动,我看不到问题所在。现在我只是想获得没有功能的布局。我有两个文件,.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()

.kv 文件是:

#:kivy 1.0

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

    Threebythreeone:
        orientation: 'horizontal'

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

运行 .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>

我正在努力学习 kivy,但是遇到这样的随机错误真是令人沮丧,谁能指出我做错了什么。

【问题讨论】:

  • 我没有使用 kivy 的经验,我只是阅读了一些 .kv 语言的文档。你的Threebythreeone 类的定义在哪里?

标签: python kivy


【解决方案1】:

您遇到错误,kivy.factory.FactoryException: Unknown class 因为您在 Threebythreeone > 小部件规则,nandx.kv

我建议您查看Programming Guide » Kivy Basics 和下面的示例。

示例

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

#: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:

输出

【讨论】:

  • 非常感谢,这完全解决了错误。我是一个如此庞大的 n00b,kivy 中的双文件东西让我很头疼,只需要了解更多信息:)
  • 相信我:您的回答比官方的回答要好得多。谢谢!
【解决方案2】:

您收到此错误是因为您在使用之前没有声明Threebythreeone。您可以像使用 Noughtsandcrosses 一样在 python 中声明它,或者在您的 kv 文件中声明它,如下面的示例所示。

有关kv 语言的更多信息,请参阅docs

另外,据我所知,Noughtsandcrosses 没有 orientation 属性。您可能应该使用BoxLayout

而且,我认为您构建按钮的方式不会按预期工作。 有关一些信息,请参阅 thisthis

以下将起作用:

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

【讨论】:

    【解决方案3】:

    只需删除
    方向:'水平'

    【讨论】:

      猜你喜欢
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-24
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多