8

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:

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:

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

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.

Rayne
  • 119
  • 1
  • 2
  • 7
  • I have no experience of using kivy, I just read some docs of the .kv language. Where is the definition of your `Threebythreeone` class? – rojeeer Sep 08 '17 at 23:09

3 Answers3

3

You encountered the error, kivy.factory.FactoryException: Unknown class <Threebythreeone> because you have a child widget Threebythreeone in the <Noughtsandcrosses> widget rule, nandx.kv

I recommend that you check out Programming Guide » Kivy Basics and the example below.

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

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

enter image description here

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Thank you so much, this totally fixed the error. I´m such a massive n00b, the dual file thing in kivy does my head in, just need to learn more about it :) – Rayne Sep 09 '17 at 08:05
  • 2
    Believe me: your answer is a much better guide than the official one. Thanks! – erickfis May 01 '20 at 20:51
0

You are getting this error because you haven't declared the Threebythreeone before using it. You can declare it in python like you have with Noughtsandcrosses or declare it in your kv file as in my example below.

See docs for more info about kv language.

Also, Noughtsandcrosses doesn't have a property orientationas far as I know. You should probably use 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()
Mox
  • 411
  • 3
  • 13
0

just remove the
orientation: 'horizontal'

Cristian M
  • 19
  • 1