AttributeError: 'BoxLayout' object has no attribute, textinput

AttributeError: 'BoxLayout' object has no attribute, textinput

[已修改] 我遇到了一个问题,当我按 button:I 总是得到下面的错误。谁能建议我如何解决这个问题?

main.py 文件:

class MainApp(MDApp):



    def build(self):
        self.dati = Builder.load_file("conf.kv")
        return Builder.load_file("conf.kv")

    def show_data(self):
        print(self.boxlay.btn_nav.scr1.classe.text)

MainApp().run()

conf.kv 文件:

BoxLayout:
    orientation:'vertical'
    id: boxlay
    btn_nav:btn_nav

    MDToolbar:
        title: 'Bottom navigation'

    MDBottomNavigation:
        id: btn_nav
        scr1:scr1

        MDBottomNavigationItem:
            id: scr1
            classe:classe
            name: 'screen 1'
            text: 'Python'
            icon: 'language-python'

            MDTextField:
                id: classe
                hint_text: "Enter Class"
                pos_hint:{'center_x': 0.5, 'center_y': 0.5}
                size_hint_x:None
                width:300
            MDRectangleFlatButton:
                text: 'Python'
                pos_hint: {'center_x': 0.5, 'center_y': 0.4}
                on_release: app.show_data()

运行 此代码我收到的错误是:

  on_release: app.show_data()
   File "main.py", line 27, in show_data
     print( AttributeError: 'NoneType' object has no attribute 'btn_nav')
 AttributeError: 'BoxLayout' object has no attribute 'classe'

感谢您的帮助

由于您定义了 ids,您可以在 python 代码中使用它们来访问从 kv 构建的小部件。所以show_data()方法可以是:

def show_data(self):
    print(self.root.ids.classe.text)

此外,我注意到您正在呼叫:

Builder.load_file("conf.kv")

在您的 build() 方法中两次。虽然这不是错误,但它可能不是您想要的。该行:

self.dati = Builder.load_file("conf.kv")

创建由以下行创建的 GUI 的完整副本:

return Builder.load_file("conf.kv")

但是,self.dati 引用的小部件树不是您的 GUI 中的小部件树,因此 self.dati 可能没有任何价值。我怀疑你的 build() 方法应该是:

def build(self):
    self.dati = Builder.load_file("conf.kv")
    return self.dati