2

I need to know the syntax for collecting user input data in text boxes in kivy, the intention is to make the login button in the bottom left hand of the screen function like it should. I want to make the program able to collect what the user puts in the password field then have an if statement determine whether or not the password was correct.

I know it's possible, I just don't know what syntax to use and the documentation doesn't talk much upon text boxes.

Python File:

import kivy
from kivy.app import App
kivy.require("1.10.1")
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.textinput import TextInput

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class ScreenManager(ScreenManager):
    pass

render = Builder.load_file("kvinterp.kv")

class MainApp(App):

    def build(self):
        return render

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

.kv file:

ScreenManager:
    Screen1:
    Screen2:

<Screen1>:
    name: "Screen1"
    Label:
        text: "Please Enter The Correct Password"
        pos_hint: {"x": .45, 'y':.9}
        size_hint: .1, .1
        font_size: 40
    TextInput:
        hint_text: "Password"
        size_hint: 0.3, 0.1
        pos_hint: {"x": 0.35, 'y': 0.5}
        multiline: False
    Button:
        text: "Login"
        on_release: app.root.current = "Screen2"
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5

<Screen2>:
    name: "Screen2"
    Label:
        text: "You've Logged In!"
    Button:
        text: "Return"
        on_release: app.root.current = "Screen1"
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Hamza Faisal
  • 115
  • 1
  • 1
  • 4

1 Answers1

2

The idea is to pass the text to a function when the button is pressed, but to identify an element an id must be established:

*.py

# ...

class Screen1(Screen):
    def validate_password(self, password):
        if password == "123456":
            self.manager.current = "Screen2"

# ...

*.kv

# ...

<Screen1>:
    name: "Screen1"
    Label:
        text: "Please Enter The Correct Password"
        pos_hint: {"x": .45, 'y':.9}
        size_hint: .1, .1
        font_size: 40
    TextInput:
        id: password_ti # <---
        hint_text: "Password"
        size_hint: 0.3, 0.1
        pos_hint: {"x": 0.35, 'y': 0.5}
        multiline: False
    Button:
        text: "Login"
        on_press: root.validate_password(password_ti.text) # <---
        size_hint: 0.17, 0.16
        pos_hint: {"x": 0, 'y':0}
        background_color: 1.23, 1.56, 1.70, .5

# ...
eyllanesc
  • 235,170
  • 19
  • 170
  • 241