Kivy 'NoneType' object has no attribute 'ids'

霸气de小男生 提交于 2020-01-11 06:03:40

问题


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 = self.parent.ids.seq_text_box
AttributeError: 'NoneType' object has no attribute 'ids'

Basically, all I'm trying to do is access the text box within the methods of the MenuBar class. I'm new to this so it's likely I'm misunderstanding something.

.py file

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput


class SequenceTextBox(TextInput):

    pass
    #...


class MenuBar(BoxLayout):

    def __init__(self, **kwargs):
        super(MenuBar, self).__init__(**kwargs)
        self.seq_text_box = self.parent.ids.seq_text_box

    def go(self):

        print(self.seq_text_box.text)


class MinuRoot(BoxLayout):
    pass


class MinuApp(App):
    pass


if __name__ == '__main__':
    MinuApp().run()

.kv file

MinuRoot:

<MinuRoot>:
    orientation: "vertical"
    MenuBar
    SequenceTextBox
        id: seq_text_box

<MenuBar>:
    height: "40dp"
    size_hint_y: None
    Button:
        text: "Go!"
        on_press: root.go()

<SequenceTextBox>:
    focus: True

I appreciate your help :)


回答1:


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:

<MinuRoot>:
    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.



来源:https://stackoverflow.com/questions/35704911/kivy-nonetype-object-has-no-attribute-ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!