Reputation: 1449
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 :)
Upvotes: 5
Views: 6110
Reputation: 5157
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.
Upvotes: 3