czolbe
czolbe

Reputation: 611

Python/Kivy Assertion Error

I obtained an Assertion error while attempting to learn BoxLayout in kivy. I cannot figure out what has went wrong.

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

    class BoxLayoutApp(App):
        def build(self):
            return BoxLayout()


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

And for the kv code:

<BoxLayout>:
    BoxLayout:
        Button:
            text: "test"
        Button:
            text: "test"
        Button:
            text: "test"
    BoxLayout:
        Button:
            text: "test"
        Button:
            text: "test"
        Button:
            text: "test"

Edit: I tried to subclass BoxLayout as suggested however, I still face an AssertionError. The full (original) error message I reproduce here:

 Traceback (most recent call last):
   File "boxlayout.py", line 12, in <module>
     BoxLayoutApp().run()
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "boxlayout.py", line 8, in build
     return BoxLayout()
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\boxlayout.py", line 131, in
__init__
     super(BoxLayout, self).__init__(**kwargs)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\layout.py", line 76, in __in
it__
     super(Layout, self).__init__(**kwargs)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\uix\widget.py", line 345, in __i
nit__
     Builder.apply(self, ignored_consts=self._kwargs_applied_init)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 451, in a
pply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 566, in _
apply_rule
     self.apply(child)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 451, in a
pply
     self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
   File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\lang\builder.py", line 464, in _
apply_rule
     assert(rule not in self.rulectx)
 AssertionError

Upvotes: 1

Views: 3382

Answers (1)

el3ien
el3ien

Reputation: 5415

Try subclassing boxlayout instead:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


class MyBoxLayout(BoxLayout):
    pass


Builder.load_string('''

<MyBoxLayout>:
    BoxLayout:
        Button:
            text: "test"
        Button:
            text: "test"
        Button:
            text: "test"
    BoxLayout:
        Button:
            text: "test"
        Button:
            text: "test"
        Button:
            text: "test"

''')


class BoxLayoutApp(App):
    def build(self):
        return MyBoxLayout()


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

The AssertionError is being thrown because you try to apply rules to the same class you are nesting.
In other words you apply a rule to a class, that it shall contain itself.
That causes issues.
Following will throw the same error.

<MyBoxLayout>:
    MyBoxLayout:

Upvotes: 5

Related Questions