Search code examples
pythonandroidwindowskivykivymd

Obtain value from KivyMD text field?


I created an application. The gui works but I cannot obtain the values from the text fields.

When I launch the application and click the add button the application crashes. Can you explain why this happens and how to fix it?

Source code :

from kivy.lang import Builder
from kivymd.app import MDApp

class Test(MDApp):

    def doThis(*kwargs):
        usa=root.ids.oft.text
        print("hello")

    def build(self):
        self.theme_cls.material_style = "M3"
        self.theme_cls.primary_palette = "Red"
        self.theme_cls.theme_style = "Light"
        return Builder.load_string(
'''
MDScreen:

    MDBottomNavigation:
        panel_color: "#FFCCD5"
        selected_color_background: "#FF758F"
        text_color_active: "black"

        MDBottomNavigationItem:
            name: 'home'
            text: 'Home'
            icon: 'home'
            md_bg_color: "#FFF0F3"

            MDAnchorLayout:
            
                anchor_x:'center'
                anchor_y:'top'
            
                MDBoxLayout:
                    orientation: 'vertical'
                    adaptive_height: True
                    
                    MDTopAppBar:
                        title: "Feet Adder"
                        elevation: 0
                        anchor_title: "left"
                        md_bg_color: "#FFF0F3"
                        specific_text_color:"black"
                          
                    MDLabel:
                        text: 'Enter the values'
                        text_size: root.width, root.height
                        padding: 0, 50, 0, 0
                        markup: True
                        halign: 'center'
                        valign: 'top'
                        
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,50,20,20
                        spacing: dp(10)    
                        
                        MDLabel:
                            text: "1st Length"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                                      
                        MDTextField:
                            hint_text: "Feet"
                            mode: "rectangle"
                            id :oft
                            
                        MDTextField:
                            hint_text: "Inches"
                            mode: "rectangle"
                            id :oin
                            
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,10,20,10
                        spacing: dp(10)
                                  
                        MDLabel:
                            text: "2st Length"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                            
                        MDTextField:
                            hint_text: "Feet"
                            mode: "rectangle"
                            id: tft
                            
                        MDTextField:
                            hint_text: "Inches"
                            mode: "rectangle"
                            id : tin
                            
                    MDAnchorLayout:
                        anchor_x:'center'
                        padding: 0,80,0,0
                             
                        MDFillRoundFlatButton:
                            text: "Add"
                            md_bg_color: "#A4133C"
                            halign: 'center'
                            on_release: app.doThis()
                            
                    MDBoxLayout:
                        orientation: 'horizontal'
                        adaptive_height: True
                        padding: 20,100,20,20
                        spacing: dp(10)
                        MDLabel:
                            text:"Result:- "
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:""
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:'"'
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:""
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None
                        MDLabel:
                            text:"'"
                            size_hint_x: None
                            width: self.texture_size[0]
                            text_size: None, None

        MDBottomNavigationItem:
            name: 'about'
            text: 'Info'
            icon: 'information'
            badge_icon: "numeric-1"

            MDLabel:
                text: 'InfoINRO'
                halign: 'center'

'''
        )

Test().run()

Solution

  • Your doThis() method needs a self argument, which you can then use to access the MDTextField, like this:

    def doThis(self):
        usa = self.root.ids.oft.text
        print("hello", usa)