Search code examples
python-3.xkivykivy-languagekivymd

How i get the data from text input in kivymd


I am new to kivy and kivymd.i want to get the text from text input .but I have facing a problem to call submit function in my python file from button .i used a navigation drawer code. should i have to define the submit function in class ContentNavigationDrawer or class TestNavigationDrawer ? Thanks in advance following is the code

.py

from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty, ListProperty
from kivymd.uix.list import OneLineIconListItem, MDList

from kivymd.app import MDApp




class ItemDrawer(OneLineIconListItem):
    icon = StringProperty()
    text_color = ListProperty((0, 0, 0, 1))


class ContentNavigationDrawer(BoxLayout):
    screen_manager = ObjectProperty()
    nav_drawer = ObjectProperty()



class TestNavigationDrawer(MDApp):
    namee = ObjectProperty()


    def build(self):
        kv= Builder.load_file("screenmd.kv")
        self.theme_cls.primary_palette = "Red"
        return kv

    def submit(self):
        print(self.namee.text)




TestNavigationDrawer().run()

.kv

<ContentNavigationDrawer>:
    orientation: "vertical"
    padding: "8dp"
    spacing: "8dp"

    AnchorLayout:
        anchor_x: "left"
        size_hint_y: None
        height: avatar.height

        Image:
            id: avatar
            size_hint: None, None
            size: "56dp", "56dp"
            source: "data/logo/kivy-icon-256.png"

    MDLabel:
        text: "KivyMD library"
        font_style: "Button"
        size_hint_y: None
        height: self.texture_size[1]

    MDLabel:
        text: "[email protected]"
        font_style: "Caption"
        size_hint_y: None
        height: self.texture_size[1]

    ScrollView:

        MDList:

            OneLineListItem:
                text: "Screen 1"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "scr 1"

            OneLineListItem:
                text: "Screen 2"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "scr 2"


Screen:

    MDToolbar:
        id: toolbar
        background_color: app.theme_cls.primary_dark
        pos_hint: {"top": 1}
        elevation: 10
        title: "Iota World"
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

    NavigationLayout:
        x: toolbar.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "scr 1"

                namee:namee


                MDTextField:
                    id:namee
                    hint_text: "Name"
                    helper_text:"forget"
                    pos_hint : {'center_x':0.5 ,'center_y':0.5}
                    size_hint_x : None
                    width: 300
                MDRectangleFlatButton:
                    text: "Submit"
                    pos_hint : {'center_x':0.5 ,'center_y':0.4}
                    text_color: 0, 0, 1, 1
                    md_bg_color: 1, 1, 0, 1
                    on_release:
                        print("success")
                        app.submit()


            Screen:
                name: "scr 2"

                MDLabel:
                    text: "Screen 2"
                    halign: "center"

        MDNavigationDrawer:
            id: nav_drawer

            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer
                id: content_drawer

error

print(self.namee.text)
 AttributeError: 'NoneType' object has no attribute 'text'

Solution

  • You need to use self.root.ids.namee.text instead of self.namee.text in your print statement.