[英]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 基础知识和下面的示例。
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()
#: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:
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.