Search code examples
pythonkivykivymd

How do I turn the text that is inputed by a user into a kivyMD text box to a variable in python file?


Im relatively new to Kivy/KivyMD. I cant figure out how to take the values from the username and password text fields and turn them into variable in my python file when the sign up button is pressed.

Currently when the Signup button is clicked it just prints "clicked".

Any help would be greatly appreciated!

Below is my python file:

from kivymd.app import MDApp
from kivymd.uix.relativelayout import MDRelativeLayout

from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen, SwapTransition

Builder.load_file('ALevelNEA.kv')

##Below are the classes for my different screens

class SignupScreen(Screen):
    pass

class MenuScreen(Screen):
    pass

class SigninScreen(Screen):
    pass

## Below are the classes for my custom buttons, widgets and text fields

class PasswordField(MDRelativeLayout):
    text = StringProperty()
    hint_text = StringProperty()
    pass

class EmailField(MDRelativeLayout):
    text = StringProperty()
    hint_text = StringProperty()
    #email_field = ObjectProperty(None)

##Below is the code for my main app

class MainApp(MDApp):
    def build(self):
        sm = ScreenManager(transition= SwapTransition())
        sm.add_widget(MenuScreen(name='menu'))
        sm.add_widget(SigninScreen(name = 'signin'))
        sm.add_widget(SignupScreen(name='signup'))
        return sm

    def RecordCheck(self):
        print("clicked")

    def AddToRecord(self):
        print("clicked")


if __name__ == '__main__':
    MainApp().run()

Below is my KV file:

#Below are my different screens

#Below is the Menu Screen

<MenuScreen>:
    Screen:
        Image:
            source: "Logonb.png"
            size_hint_x: 0.4
            size_hint_y: 0.6
            allow_stretch: True
            pos_hint: {"center_x": 0.5, "center_y": 0.6 }

        MDFillRoundFlatButton:
            id: SignIn
            text: "Sign in with email"
            pos_hint: {"center_x": 0.5, "center_y": 0.2}
            md_bg_color: 0.3607, 0.3882, 1
            size_hint: 0.225, 0
            on_release:
                root.manager.current = "signin"

        MDRoundFlatButton:
            text: "Sign up"
            pos_hint: {"center_x": 0.5, "center_y": 0.12}
            text_color: 0.3607, 0.3882, 1
            line_color: 0, 0, 0
            on_release:
                root.manager.current = "signup"

        MDLabel:
            text: "by continuing you agree to the"
            font_size: 15
            halign: 'center'
            pos_hint: {"center_y": 0.07}

        MDTextButton:
            text: "Terms and Conditions"
            font_size: 15
            pos_hint: {"center_x": 0.5, "center_y": 0.0525}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

# Below is my sign in Screen

<SigninScreen>:
    Screen:
        MDIconButton:
            icon: "keyboard-backspace"
            pos_hint: {"center_x": 0.35, "center_y": 0.95}
            on_release:
                root.manager.current = "menu"

        Image:
            source: "LoginImage.png"
            size_hint_x: 0.4
            size_hint_y: 0.6
            allow_stretch: True
            pos_hint: {"center_x": 0.5, "center_y": 0.7 }

        MDLabel:
            text: "Sign in"
            halign: "center"
            pos_hint: {"center_x": 0.387, "center_y": 0.45}
            font_size: 30
            font_style: "H5"

        EmailField:
            hint_text: "Email ID"
            pos_hint: {"center_x": 0.5, "center_y": 0.375}
            size_hint_x: None
            width: "250dp"

        PasswordField:
            size_hint_x: None
            width: "250dp"
            hint_text: "Password"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}

        MDTextButton:
            text: "Forgot password?"
            font_size: 15
            pos_hint: {"center_x": 0.62, "center_y": 0.255}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

        MDFillRoundFlatButton:
            text: "Sign in"
            pos_hint: {"center_x": 0.5, "center_y": 0.2}
            md_bg_color: 0.3607, 0.3882, 1
            size_hint: 0.225, 0
            on_release: app.RecordCheck()

        MDLabel:
            text: "by continuing you agree to the"
            font_size: 15
            halign: 'center'
            pos_hint: {"center_y": 0.15}

        MDTextButton:
            text: "Terms and Conditions"
            font_size: 15
            pos_hint: {"center_x": 0.5, "center_y": 0.1325}
            theme_text_color: "Custom"
            text_color: 0.3607, 0.3882, 1

#Below is the sign up screen
<SignupScreen>:

    Screen:
        EmailField:
            hint_text: "Email"
            pos_hint: {"center_x": 0.5, "center_y": 0.375}
            size_hint_x: None
            width: "250dp"
            on_text: root.email = self.text

        PasswordField:
            size_hint_x: None
            width: "250dp"
            hint_text: "Password"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            on_text: root.password = self.text

        MDRoundFlatButton:
            text: "Sign up"
            pos_hint: {"center_x": 0.5, "center_y": 0.12}
            text_color: 0.3607, 0.3882, 1
            line_color: 0, 0, 0
            on_release: app.AddToRecord()

#Custom buttons and text fields are located below

<PasswordField>:
    size_hint_y: None
    height: text_field.height

    MDTextField:
        id: text_field
        hint_text: root.hint_text
        text: root.text
        password: True
        icon_left: "lock"

    MDIconButton:
        icon: "eye-off"
        pos_hint: {"center_y": .5}
        pos: text_field.width - self.width + dp(8), 0
        theme_text_color: "Hint"
        on_release:
            self.icon = "eye" if self.icon == "eye-off" else "eye-off"
            text_field.password = False if text_field.password is True else True

<EmailField>:
    size_hint_y: None
    height: email_field.height

    MDTextField:
        id: email_field
        hint_text: root.hint_text
        text: root.text
        icon_left: "email"


Solution

  • Add id attribute both to email and password widgets:

        EmailField:
            id: email_widget
    
        PasswordField:
            id: pass_widget
    

    And you can pass any additional parameters to your method, for example replace:

    on_release: app.AddToRecord()
    

    with

    on_release: app.AddToRecord(root.ids.email_widget.ids.email_field.text, root.ids.pass_widget.ids.text_field.text)
    

    then change your AddToRecord method definition to:

    def AddToRecord(self, email, passw):
        print("clicked")
        print(f'email: {email}, password: {passw}')