Kivy错误:引发FactoryException('未知类<%s>'%name)

时间:2017-09-08 22:16:33

标签: python kivy

我搜索过并搜索过,而我却得不到它。根据我从发布的答案中理解,问题是类没有定义或拼写错误,但我已经来回通过我的代码,我看不到问题。现在我只是试图让布局没有功能。我有两个文件,.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,但是这样的随机错误是如此令人沮丧,任何人都可以指出我做错了。

3 个答案:

答案 0 :(得分:0)

您收到此错误是因为在使用之前您尚未声明python run.py “text message”。您可以像使用Threebythreeone一样在python中声明它,或者在Noughtsandcrosses文件中声明它,如下例所示。

有关kv语言的详情,请参阅docs

另外,就我所知,kv没有属性Noughtsandcrosses。您应该使用orientation

而且,我并不认为你构建按钮的方式会像你期望的那样工作。  有关详细信息,请参阅thisthis

以下工作:

BoxLayout

答案 1 :(得分:0)

您遇到错误,kivy.factory.FactoryException:未知类&lt; Threebythreeone&gt;因为您在 &lt; Noughtsandcrosses&gt; 小部件规则中有一个子小部件 Threebythreeone ,nandx.kv

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

实施例

main.py

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

nandx.kv

- (void)viewDidLoad {
    mapVC = [[CollectionMapViewController alloc] init];
}

输出

enter image description here

答案 2 :(得分:0)

只需删除
方向:'水平'