Kivy 'NoneType' object has no attribute 'ids'

后端 未结 1 1719
有刺的猬
有刺的猬 2021-01-06 10:51

I am receiving the following error in my Kivy application but I am not sure why and how to fix it:

File \"main.py\", line 16, in __init__
self.seq_text_box =         


        
1条回答
  •  一整个雨季
    2021-01-06 11:21

    You can store seq_text_box as an ObjectProperty of MenuBar and set it in the kv file:

    class MenuBar(BoxLayout):
        seq_text_box = ObjectProperty()
        def go(self):
            print(self.seq_text_box.text)
    

    and in the kv file:

    :
        orientation: "vertical"
        MenuBar:
            seq_text_box: seq_text_box
        SequenceTextBox:
            id: seq_text_box
    

    The reason you receive the error is because in the constructor the ids haven't been populated from the rules specified in the kv file.

    If you do want to use a plain attribute, you can schedule a Clock event:

    class MenuBar(BoxLayout):
        def __init__(self, **kwargs):
            super(MenuBar, self).__init__(**kwargs)
            Clock.schedule_once(self.init_seq_text_box, 0)
    
        def init_seq_text_box(self, *args):
            self.seq_text_box = self.parent.ids.seq_text_box
    

    This will schedule a call to init_eq_text_box for the next frame, when ids will be populated.

    0 讨论(0)
提交回复
热议问题