Ender Look
Ender Look

Reputation: 2391

How to print value in a TextInput when a Button is pressed? (Kivi)

I'm making a logging GUI but I can't find how to print the values of two TextInputs when a button is pressed.

This is my .py:

import kivy

from kivy.app               import App
from kivy.uix.boxlayout     import BoxLayout

#[...]  

class Login_Screen(BoxLayout):
    def register(self):
        print('Hi! I want here have the user and password, so later I can make a proper register function.')
    pass

class MainApp(App):
    def build(self):
        return Login_Screen()

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

This is my .kv: (The important part is in the last line.)

#[...]

<Login_Screen>:
    #[...]

    BoxLayout:
        AnchorLayout:
            #[...]
            TextInput01: # Come from @TextInput
                id: user_input              

    BoxLayout:
        #[...]
        AnchorLayout:
            TextInput01: # Come from @TextInput
                id: password_input

    BoxLayout:
        Button01: # Come from @Button
            id: login
            text: 'Login'
        Button01: # Come from @Button
            id: register
            text: 'Register'
            on_press: root.register()

#[...] is code that I delete in order to make this more clear.

I want to print password_input value and user_input value when I press register.
The code must be in the .py file because I don't want exactly only print the values, I want to make something more functional but this in an example for me to understand.

Upvotes: 0

Views: 1172

Answers (1)

ikolim
ikolim

Reputation: 16001

Please refer to the snippet and example below.

Snippet

def register(self):
    print("user: ", self.ids.user_input.text)
    print("password: ", self.ids.password_input.text)

Example

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class Login_Screen(BoxLayout):

    def register(self):
        print("user: ", self.ids.user_input.text)
        print("password: ", self.ids.password_input.text)


class MainApp(App):

    def build(self):
        return Login_Screen()


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

main.kv

#:kivy 1.10.0

<Login_Screen>:
    BoxLayout:
        AnchorLayout:
            TextInput:
                id: user_input

    BoxLayout:
        AnchorLayout:
            TextInput:
                id: password_input

    BoxLayout:
        Button:
            id: login
            text: 'Login'
        Button:
            id: register
            text: 'Register'
            on_press: root.register()

Output

enter image description here

Upvotes: 2

Related Questions