Search code examples
pythonandroidkivykivy-languagekivymd

AttributeError: 'BoxLayout' object has no attribute, textinput


[Modified] I've got a problem when I press the button:I always get the error below. Can anyone suggest me how to solve this problem?

main.py file:

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 file:

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()

running this code the error I receive is:

  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'

Thanks for helping


Solution

  • Since you have ids defined, you can use them in your python code to access the widgets built from your kv. So the show_data() method can be:

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

    Also, I notice that you are calling:

    Builder.load_file("conf.kv")
    

    twice in your build() method. While that is not an error, it is likely not what you want. The line:

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

    Creates a complete copy of the GUI that is created by the line:

    return Builder.load_file("conf.kv")
    

    however, the widget tree referred to by self.dati is not the widget tree that is in your GUI, so self.dati is probably of no value. I suspect your build() method should be:

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