Kivy-如何获取TextInput的内容

问题描述 投票:0回答:1

我不知道如何从文本输入框中获取信息。其他问题无济于事,所以我希望有人可以在我的特定情况下为我提供帮助。我正在使用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"

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

python python-3.x kivy python-3.7 kivy-language
1个回答
0
投票

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"
© www.soinside.com 2019 - 2024. All rights reserved.