Kivy'NonType'对象没有属性'ids'

时间:2016-02-29 16:46:18

标签: python kivy

我在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类方法中的文本框。我是新手,所以我很可能误解了一些事情。

.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

感谢您的帮助:)。

1 个答案:

答案 0 :(得分:0)

您可以将seq_text_box存储为MenuBar 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,此时将填充function IcmpSendEcho(ICMPHandle: Cardinal; DestinationAddress: Integer; RequestData: Pointer; RequestSize: Word; RequestOptions: Pointer; ReplyBuffer: Pointer; ReplySize: Cardinal; TimeOut: Cardinal): Cardinal; stdcall; external 'icmp.dll'; function TForm1.Ping(IPAddress: string; TimeOut: Integer): Integer; var ICMPHandle: Cardinal; DestinationAddress: Integer; ReplyBuffer: pICMPEchoReply; RequestData: array[0..31] of AnsiChar; ReplySize: Cardinal; begin ICMPHandle := IcmpCreateFile; DestinationAddress := inetaddr(pansichar(AnsiString(IPAddress))); RequestData := 'data buffer'; ReplySize := SizeOf(ticmpechoreply) + SizeOf(RequestData); ReplyBuffer := AllocMem(ReplySize); IcmpSendEcho(ICMPHandle, DestinationAddress, @RequestData, SizeOf(requestdata), nil, ReplyBuffer, ReplySize, TimeOut); Result := replybuffer.Status; FreeMem(ReplyBuffer); end;