Need help to get data from TextFields

28 views
Skip to first unread message

Yash Potdar

unread,
May 26, 2023, 11:02:58 AM5/26/23
to Kivy users support
I want to get data from the TextField in the password manager screen and print it on the terminal, but I am unable to do it. Pls help.
P.S.: I have highlighted the required TextFields and Submit button.

<main.kv>
<DrawerClickableItem@MDNavigationDrawerItem>
    focus_color: "#e7e4c0"
    unfocus_color: "#fffcf4"
MDScreen:
    MDNavigationLayout:
        ScreenManager:
            MDScreen:
                MDBoxLayout:
                    orientation: "vertical"
                    MDBoxLayout:
                        adaptive_height: True
                        md_bg_color: "#fffcf4"
                        padding: "12dp"
                        MDLabel:
                            text: "CyberShield"
                            adaptive_height: True
                            pos_hint: {"center_y": .5}
                    MDBoxLayout:
                        MDNavigationRail:
                            id: navigation_rail
                            md_bg_color: "#fffcf4"
                            selected_color_background: "#e7e4c0"
                            ripple_color_item: "#e7e4c0"
                            MDNavigationRailMenuButton:
                                on_release: nav_drawer.set_state("open")
                            MDNavigationRailItem:
                                text: "Home"
                                icon: "home"
                                on_release:
                                    screen_manager.current = "main_window"
                            MDNavigationRailItem:
                                text: "Passwords"
                                icon: "key"
                                on_release:
                                    screen_manager.current = "passwordmanager"
                        ScreenManager:
                            id: screen_manager
                            main_window: main_window
                            settings: settings
                            passwordmanager: passwordmanager
                            Screen:
                                id: main_window
                                name: "main_window"
                                MDLabel:
                                    text:"Hello"
                                    halign:"center"
                            Screen:
                                id: passwordmanager
                                name: "passwordmanager"
                                website: website
                                load: load
                                username: username
                                passwords: passwords
                                submit: submit
                                MDGridLayout:
                                    rows:3
                                    cols:2
                                    MDTextField:
                                        id: website
                                        multiline:False
                                        hint_text:"Enter Website"
                                        size_hint_x:0.5
                                        icon_left:"web"
                                    MDIconButton:
                                        id: load
                                        icon: "plus-box-multiple-outline"
                                    MDTextField:
                                        id: username
                                        multiline:False
                                        hint_text: "Enter Username"
                                        size_hint_x:0.5
                                        icon_left: "account-circle-outline"
                                    MDIconButton:
                                        id:pencil
                                        icon:"pencil"
                                        theme_text_color:"Hint"
                                    MDTextField:
                                        id: passwords
                                        multiline:False
                                        hint_text: "Enter Password"
                                        size_hint_x: 0.5
                                        password: True
                                        icon_left: "key-variant"
                                    MDIconButton:
                                        id: showpassword
                                        icon: "eye-off"
                                        text:"Show Password"
                                        pos_hint: {"center_y": 0.5}
                                        theme_text_color: "Hint"
                                        on_release:
                                            self.icon = "eye" if self.icon == "eye-off" else "eye-off"
                                            passwords.password = False if passwords.password is True \
                                            else True
                                MDFillRoundFlatButton:
                                    id: submit
                                    text:"Submit"
                                    pos_hint: {"center_x":.5, "center_y":.5}
                                    on_release:
                                        app.login_data()
    MDNavigationDrawer:
        id: nav_drawer
        radius: 0, 16, 16, 0
        md_bg_color: "#fffcf4"
        elevation: 2
        width: "240dp"
        MDNavigationDrawerMenu:
            MDBoxLayout:
                orientation: "vertical"
                adaptive_height: True
                spacing: "12dp"
                padding: 0, 0, 0, "12dp"
                MDIconButton:
                    icon: "arrow-left"
                    on_release: nav_drawer.set_state("close")
                MDBoxLayout:
                    adaptive_height: True
                    padding: "12dp", 0, 0, 0
            DrawerClickableItem:
                text: "Home"
                icon: "home"
                on_release:
                    screen_manager.current = "main_window"
            DrawerClickableItem:
                text: "Passwords"
                icon: "key"
                on_release:
                    screen_manager.current = "passwordmanager"

<main.py>
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import ObjectProperty
class CyberShieldApp(MDApp):
    screen_manager  = ObjectProperty(None)
    def build(self):
        self.screen = Builder.load_file("main.kv")
        return self.screen
    def login_data(self):
        website = self.screen.screen_manager.passwordmanager.website.text
        username = self.screen.screen_manager.passwordmanager.username.text
        password = self.screen.screen_manager.passwordmanager.password.text
        print(website)
        print(username)
        print(password)
if __name__ == "__main__":
    app = CyberShieldApp()
    app.run()

ElliotG

unread,
May 26, 2023, 8:01:46 PM5/26/23
to Kivy users support

I have modified your code below.  I put the kv in the same file to simplify uploading.  I commented out a few of the lines, that caused problems or were unnecessary.  The login_data() method was changed to use get_screen()

 def login_data(self):
        p = self.root.ids.screen_manager.get_screen("passwordmanager")
        website = p.website.text
        username = p.username.text
        password = p.passwords.text
        print(website)
        print(username)
        print(password)

In this context:
self.root.ids.screen_manager.get_screen("passwordmanager")
self is the App
root is the root widget
ids.screen_manager gets the screen_manager widget from the ids dictionary
get_screen("passwordmanager") returns the requested screen

The complete code:

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

kv = """
                            # main_window: main_window
                            # settings: settings
                            # passwordmanager: passwordmanager

                            Screen:
                                id: main_window
                                name: "main_window"
                                MDLabel:
                                    text:"Hello"
                                    halign:"center"
                            Screen:
class CyberShieldApp(MDApp):
    screen_manager = ObjectProperty(None)

    def build(self):
        return Builder.load_string(kv)

    def login_data(self):
        p = self.root.ids.screen_manager.get_screen("passwordmanager")
        website = p.website.text
        username = p.username.text
        password = p.passwords.text

        print(website)
        print(username)
        print(password)


if __name__ == "__main__":
    app = CyberShieldApp()
    app.run()

Yash Potdar

unread,
May 28, 2023, 2:56:30 AM5/28/23
to Kivy users support
Thank for your support, the code works now.
Reply all
Reply to author
Forward
0 new messages