Kivy - 如何获取 TextInput 的内容

Kivy - How to get contents of TextInput

我不知道如何从文本输入框中获取信息。其他问题没有帮助,所以我希望有人能在我的具体情况下帮助我。我正在使用 Kivy v.1.11.1 和 Python 3.7.

这是我的代码:

user.py

"""User-end software for signup/account data."""
from kivy.app import App
from kivy.uix.widget import Widget


class LoginScreen(Widget):
    """Class for login screen contents."""

    pass  # Python code for TextInput boxes should go here, I think.


class UserApp(App):
    """Main app."""

    def build(self):
        """Build app."""
        return LoginScreen()


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

user.kv

#:kivy 1.11.1

<LoginScreen>:
    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top + 20
        text: "Offbox Insurance"
    Label:
        font_size: 64
        center_x: root.width / 2
        top: root.top - 30
        text: "Log in"

    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top - 140
        text: "Email"
    TextInput:
        id: email_input
        font_size: 24
        height: 40
        width: root.width * 5 / 7
        center_x: root.width / 2
        top: root.top - 216
        multiline: False

    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top - 240
        text: "Password"
    TextInput:
        id: password_input
        font_size: 24
        height: 40
        width: root.width * 5 / 7
        center_x: root.width / 2
        top: root.top - 316
        multiline: False

    Button:
        font_size: 20
        height: 50
        center_x: root.width / 2
        top: root.top - 380
        text: "Log in"

    Label:
        font_size: 16
        center_x: root.width / 2
        top: root.height / 12 + 75
        text: "Don't have an account?"
    Button:
        font_size: 16
        height: 36
        center_x: root.width / 2
        top: root.height / 12 + 5
        text: "Sign up"

我需要将两个 TextInput 存储在它们自己的变量中,以便我可以分别处理它们。

TextInput 文本可以通过 StringProperty 映射。还假设您想在按下任何按钮时获取该信息,那么您必须使用 on_press.

调用函数
"""User-end software for signup/account data."""
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty


class LoginScreen(Widget):
    """Class for login screen contents."""

    email = StringProperty()
    password = StringProperty()

    def login(self):
        print("email:", self.email, "password:", self.password)


class UserApp(App):
    """Main app."""

    def build(self):
        """Build app."""
        return LoginScreen()


if __name__ == "__main__":
    UserApp().run()
#:kivy 1.11.1

<LoginScreen>:
    email: email_input.text
    password: password_input.text
    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top + 20
        text: "Offbox Insurance"
    Label:
        font_size: 64
        center_x: root.width / 2
        top: root.top - 30
        text: "Log in"
    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top - 140
        text: "Email"
    TextInput:
        id: email_input
        font_size: 24
        height: 40
        width: root.width * 5 / 7
        center_x: root.width / 2
        top: root.top - 216
        multiline: False
    Label:
        font_size: 20
        center_x: root.width / 2
        top: root.top - 240
        text: "Password"
    TextInput:
        id: password_input
        font_size: 24
        height: 40
        width: root.width * 5 / 7
        center_x: root.width / 2
        top: root.top - 316
        multiline: False
        password: True
    Button:
        font_size: 20
        height: 50
        center_x: root.width / 2
        top: root.top - 380
        text: "Log in"
        on_press: root.login()
    Label:
        font_size: 16
        center_x: root.width / 2
        top: root.height / 12 + 75
        text: "Don't have an account?"
    Button:
        font_size: 16
        height: 36
        center_x: root.width / 2
        top: root.height / 12 + 5
        text: "Sign up"