Kivy'NoneType'对象没有属性'ids' [英] Kivy 'NoneType' object has no attribute 'ids'
问题描述
我在Kivy应用程序中收到以下错误,但是我不确定为什么以及如何解决它:
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'
基本上,我要做的就是访问MenuBar
类的方法中的文本框.我是新手,所以可能是我误会了一些东西.
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文件
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文件
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
感谢您的帮助:)
推荐答案
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)
并在kv
文件中:
<MinuRoot>:
orientation: "vertical"
MenuBar:
seq_text_box: seq_text_box
SequenceTextBox:
id: seq_text_box
收到错误的原因是因为在构造函数中ids
尚未根据kv
文件中指定的规则进行填充.
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.
如果您确实想使用普通属性,则可以安排Clock
事件:
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
当填充ids
时,这将安排对下一帧的呼叫init_eq_text_box
.
This will schedule a call to init_eq_text_box
for the next frame, when ids
will be populated.
这篇关于Kivy'NoneType'对象没有属性'ids'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!