gpt4 book ai didi

python - Kivy - 如何获取 TextInput 的内容

转载 作者:行者123 更新时间:2023-12-01 06:51:45 25 4
gpt4 key购买 nike

我不知道如何从文本输入框中获取信息。其他问题没有帮助,所以我希望有人可以帮助我解决我的具体情况。我使用的是 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"

关于python - Kivy - 如何获取 TextInput 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com