我在嘗試學習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
它有助於顯示實際的錯誤。 – Keith