【问题标题】:"AttributeError: 'NoneType' object has no attribute 'insert'"“AttributeError:‘NoneType’对象没有属性‘插入’”
【发布时间】:2019-12-10 05:46:29
【问题描述】:

我真的很困惑为什么只有我的一个文本输入字段会发生这种情况。我还有其他多个工作正常,虽然我已经研究过类似的问题,但我还没有找到适合我的情况的答案。我得到的错误是“AttributeError:'NoneType'对象没有属性'insert'”

main.py

class PredictEstimate(Screen):
    children = ObjectProperty(None)

    def submitPatient(self):
        childrenText = self.children.text
        print("Children Text: ", childrenText)


class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("login.kv")
sm = WindowManager()

screens = [PredictEstimate(name="predict")]
for screen in screens:
    sm.add_widget(screen)

class MyMainApp(App):
    def build(self):
        return sm

if __name__ == "__main__":
    MyMainApp().run()

.kv 文件

<PredictEstimate>:

    children: children


    FloatLayout:

        Label:
            text:"Number of Children: "
            font_size: (40)
            pos_hint: {"x":0.05, "y":0.45}
            size_hint: 0.4, 0.15

        TextInput:
            id: children
            font_size: (50)
            multiline: False
            pos_hint: {"x": 0.5, "y":0.45}
            size_hint: 0.4, 0.1

        Button:
            pos_hint:{"x":0.68, "y": 0.05}
            size_hint:0.3,0.1
            font_size: (50)
            background_color: .1, .1, .1, .1
            text: "Submit"
            on_release:
                root.submitPatient()

【问题讨论】:

  • age: ObjectProperty(None)更改为age = ObjectProperty(None)
  • 我尝试更改它,但现在我在 add_widget self.children.insert(0, widget) AttributeError 中收到错误“文件”/kivy/uix/widget.py”,第 632 行: 'NoneType' 对象没有属性 'insert'"
  • 你知道那可能指的是什么吗?
  • 不,如果您需要帮助,请提供minimal reproducible example
  • 我只改了你告诉我的那一行,但我在上面的代码中编辑了它

标签: python kivy textfield


【解决方案1】:

说明:

Widget 类具有 children 属性,用于存储子小部件,因此任何从 Widget 继承的类(例如 Screen 和 PredictEstimate)都将拥有它,但您使用 None 覆盖它会生成错误表示。

解决办法:

不要使用子属性,而是该属性的另一个名称:

ma​​in.py

from kivy.app import App

from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen


class PredictEstimate(Screen):
    text_input = ObjectProperty(None)

    def submitPatient(self):
        childrenText = self.text_input.text
        print("Children Text: ", childrenText)


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("login.kv")
sm = WindowManager()

screens = [PredictEstimate(name="predict")]
for screen in screens:
    sm.add_widget(screen)


class MyMainApp(App):
    def build(self):
        return sm


if __name__ == "__main__":
    MyMainApp().run()

login.kv

<PredictEstimate>:
    text_input: text_input

    FloatLayout:
        Label:
            text:"Number of Children: "
            font_size: (40)
            pos_hint: {"x":0.05, "y":0.45}
            size_hint: 0.4, 0.15

        TextInput:
            id: text_input
            font_size: (50)
            multiline: False
            pos_hint: {"x": 0.5, "y":0.45}
            size_hint: 0.4, 0.1

        Button:
            pos_hint:{"x":0.68, "y": 0.05}
            size_hint:0.3,0.1
            font_size: (50)
            background_color: .1, .1, .1, .1
            text: "Submit"
            on_release:
                root.submitPatient()

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    相关资源
    最近更新 更多