KivyMD ContentNavigationDrawer acces Textvariable from Screen

Viewed 29

I'm quite new to Python and Kivy. I'm Building an app for Android.

I'm using a modifyed version of the Navigation Drawer example from here: https://kivymd.readthedocs.io/en/latest/components/navigationdrawer/

I want to get the Value of the Textfield from scr 1 vorname_input. I get this before without switchable screens. But i can't figure out how it works with the different screens. I googled for several hours and tryed so much things but i can't get it working.

If I use print (self.root.ids.vorname_input.text) its empty.

KV File:

<ContentNavigationDrawer>

    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"


MDScreen:

    MDTopAppBar:
        pos_hint: {"top": 1}
        elevation: 4
        title: "MDNavigationDrawer"
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

    MDNavigationLayout:

        MDScreenManager:
            id: screen_manager

            MDScreen:
                name: "scr 1"
                BoxLayout:
                    orientation: "vertical"
                    #size: root.width, root.height
                    spacing: "25dp"
                    padding: [12, 80, 12, 12]

                    MDTextField:
                        id: vorname_input
                        hint_text: "Vorname"
                        mode: "rectangle"
                        multiline: False
                        icon_left: 'account'
                        helper_text_mode: 'on_error'
                        helper_text: ''
                        mode: "fill"
                        fill_color: 0, 0, 0, .4
                     MDFlatButton:
                        #size_hint: (1, .5)
                        font_size: 25
                        text: "Kunde Speichern"
                        on_press: app.save_cust_to_db()

                        md_bg_color: app.theme_cls.primary_color
                        theme_text_color: "Custom"
                        text_color: 0.93, 0.93, 0.93, 1

            MDScreen:
                name: "scr 2"

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

        MDNavigationDrawer:
            id: nav_drawer
            radius: (0, 16, 16, 0)

            ContentNavigationDrawer:
                screen_manager: screen_manager
                nav_drawer: nav_drawer

Py File:

from kivy.lang import Builder
from kivy.properties import ObjectProperty

from kivymd.app import MDApp
from kivymd.uix.scrollview import MDScrollView

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


class Example(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Orange"
        self.theme_cls.theme_style = "Dark"
        return Builder.load_string(KV)
 
    def save_cust_to_db(self):
        print (self.root.ids.vorname_input.text)


Example().run()
1 Answers

I found my mistake. In the KV file downwards i had another label with the same id. So it gets always overwritten and printet nothing because this textinputfield was empty.

It was a common copy and paste misstake :)

Related