1

次の問題が発生しました。

サンプル (mainok.py、testok.kv) が正常に起動し、

$ cat mainok.py
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class BuddyList(BoxLayout):
    list_view = ObjectProperty()
    def __init__(self, **kwargs):
        super(BuddyList, self).__init__(**kwargs)
        assert(self.list_view is not None)
        self.list_view.adapter.data = ['v1', 'v2']
    def roster_converter(self, index, txt):
        result = {
            "text": "%s %d" % (txt, index),
            'size_hint_y': None,
            'height': 25
        }
        return result
class TestForm(BoxLayout):
    text_input = ObjectProperty()

    def __init__(self, **kwargs):
        super(TestForm, self).__init__(**kwargs)
        self.buddy_list = BuddyList()
        self.add_widget(self.buddy_list)
class TestokApp(App):
    pass
if __name__ == '__main__':
    TestokApp().run()

$ cat testok.kv
#:import la kivy.adapters.listadapter
#:import ku kivy.uix.listview
TestForm:
<BuddyList>:
   list_view: list_view
    ListView:
        id: list_view
        adapter:
            la.ListAdapter(data=[], cls=ku.ListItemButton,
            args_converter=root.roster_converter)
<TestForm>:
    orientation: 'vertical'
    BoxLayout:
        Label:
            text: 'the label'

例 (mainko.py、testko.kv) はクラッシュします:

$ cat mainko.py
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class BuddyList(BoxLayout):
    list_view = ObjectProperty()
    def __init__(self, **kwargs):
        super(BuddyList, self).__init__(**kwargs)
        assert(self.list_view is not None)
        self.list_view.adapter.data = ['v1', 'v2']
    def roster_converter(self, index, txt):
        result = {
            "text": "%s %d" % (txt, index),
            'size_hint_y': None,
            'height': 25
        }
        return result
class TestForm(BoxLayout):
    text_input = ObjectProperty()
    def __init__(self, **kwargs):
        super(TestForm, self).__init__(**kwargs)
class TestkoApp(App):
    pass
if __name__ == '__main__':
    TestkoApp().run()

$ cat testko.kv
#:import la kivy.adapters.listadapter
#:import ku kivy.uix.listview
TestForm:
<BuddyList>:
    list_view: list_view
    ListView:
        id: list_view
        adapter:
            la.ListAdapter(data=[], cls=ku.ListItemButton,
            args_converter=root.roster_converter)
<TestForm>:
    orientation: 'vertical'
    BoxLayout:
        Label:
            text: 'the label'
    BuddyList:

次のエラーでクラッシュします

  assert(self.list_view is not None)
     AssertionError

2つの違いは次のとおりです。

$ diff -u mainko.py ../ok/mainok.py
--- mainko.py       2013-10-23 14:11:26.976723764 +0200
+++ ../ok/mainok.py 2013-10-23 14:12:34.976841090 +0200
@@ -26,10 +26,13 @@

 def __init__(self, **kwargs):
     super(TestForm, self).__init__(**kwargs)
+        self.buddy_list = BuddyList()
+        self.add_widget(self.buddy_list)

-class TestkoApp(App):
+
+class TestokApp(App):
     pass

 if __name__ == '__main__':
-    TestkoApp().run()
+    TestokApp().run()

$ diff -u testko.kv ../ok/testok.kv
--- testko.kv       2013-10-23 14:10:11.948299722 +0200
+++ ../ok/testok.kv 2013-10-23 14:16:51.352688453 +0200
@@ -16,5 +16,4 @@
 BoxLayout:
     Label:
         text: 'the label'
-    BuddyList:

最初のものは成功し、2番目のものは失敗する理由は何ですか?

ありがとう、

4

1 に答える 1