2017-04-16 77 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