2017-04-16 73 views
1

我在尝试学习kivy中的BoxLayout时获得了Assertion错误。我无法弄清楚出了什么问题。Python/Kivy声明错误

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

而对于KV代码:

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

编辑:我试图子类的BoxLayout的建议不过,我仍然面临着一个AssertionError。完整的(原)错误信息我这里重现:

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 
+0

它有助于显示实际的错误。 – Keith

回答

3

尝试子类的BoxLayout代替:

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

AssertionError被抛出,因为你试图规则适用于你嵌套同一类。
换句话说,您将规则应用于类,它应包含自身。
这会导致问题。
以下将引发同样的错误。

<MyBoxLayout>: 
    MyBoxLayout: 
+0

如果您可以添加为什么应该修复错误,那将是非常好的。更新了 – syntonym

+0

@syntonym – EL3PHANTEN