Kivy如何获取TextInpu的内容

2024-09-28 17:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何从文本输入框中获取信息。其他问题没有帮助,所以我希望有人能帮助我在我的具体情况。我使用的是kivyv.1.11.1和python3.7。你知道吗

这是我的密码:

用户.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()

用户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都存储在它们自己的变量中,这样我就可以分别处理它们。你知道吗


Tags: text用户appforsizetoproottextinput
1条回答
网友
1楼 · 发布于 2024-09-28 17:28:27

TextInput文本可以通过StringProperty映射。另外,假设您希望在按下任何按钮时获得该信息,那么您必须使用on\u 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"

相关问题 更多 >