Kivy 'NoneType' 对象没有属性 'ids'
Kivy 'NoneType' object has no attribute 'ids'
我在我的 Kivy 应用程序中收到以下错误,但我不确定为什么以及如何修复它:
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
class 方法中的文本框。我是新手,所以我可能误解了什么。
.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
感谢您的帮助:)
您可以将 seq_text_box
存储为 MenuBar
的 ObjectProperty
并将其设置在 kv
文件中:
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
文件中指定的规则填充。
如果您确实想使用普通属性,您可以安排一个 Clock
事件:
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
这将为下一帧安排对 init_eq_text_box
的调用,届时将填充 ids
。
我在我的 Kivy 应用程序中收到以下错误,但我不确定为什么以及如何修复它:
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
class 方法中的文本框。我是新手,所以我可能误解了什么。
.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
感谢您的帮助:)
您可以将 seq_text_box
存储为 MenuBar
的 ObjectProperty
并将其设置在 kv
文件中:
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
文件中指定的规则填充。
如果您确实想使用普通属性,您可以安排一个 Clock
事件:
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
这将为下一帧安排对 init_eq_text_box
的调用,届时将填充 ids
。