gpt4 book ai didi

android - 如何使用 kivy StringProperty?

转载 作者:太空狗 更新时间:2023-10-29 13:11:39 24 4
gpt4 key购买 nike

我想实现一个 kivy 应用程序,它有两个屏幕(由屏幕管理器管理)。在第一个屏幕(称为 LoginScreen)上有两个 TextInput 字段和一个按钮。在第二个屏幕上,我有两个标签,我想显示在第一个屏幕上输入的值。单击按钮后更改屏幕。
我设法将这些字段绑定(bind)在一起,以便在第二个屏幕上显示值。但是,我想在第二个屏幕中“处理”这些值,不幸的是,我没能做到。
我所说的“处理”是指,我想使用自定义构建的类(正在运行)登录我的电子邮件帐户并列出我看不见的电子邮件(由该类的一个功能提供) 在一个 kivy 列表中。
有人可以告诉我如何获取输入的值并使用它们来创建我的类吗?
如果有任何帮助,我将不胜感激!
我的.py文件:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ListProperty, StringProperty

class MainScreenManager(ScreenManager):
pass

class LoginScreen(Screen):
entered_email_address = StringProperty('')
entered_password = StringProperty('')

def check_input(self):
text_input_email = self.ids['ti_email'].text
text_input_password = self.ids['ti_password'].text

self.entered_email_address = text_input_email
self.entered_password = text_input_password

"""
the values in this part are printed out
print self.manager
print self.manager.screens
print self.manager.get_screen('HomeScreen').email_address
print self.manager.get_screen('HomeScreen').password
"""

self.manager.current = 'HomeScreen'

class HomeScreen(Screen):
email_address = StringProperty()
password = StringProperty()

def __init__(self, *args, **kwargs):
super(HomeScreen, self).__init__(*args, **kwargs)

class TutorialApp(App):

def build(self):
return MainScreenManager()


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

我的 .kv 文件:

<MainScreenManager>:
id: screen_manager

LoginScreen:
id: login_screen
name: 'LoginScreen'
manager: screen_manager

HomeScreen:
id: home_screen
name: 'HomeScreen'
email_address: login_screen.entered_email_address
password: login_screen.entered_password

<LoginScreen>
BoxLayout:
orientation: 'vertical'
TextInput:
id: ti_email
multiline: False
size_hint_y: None
height: 40
font_size: 25

TextInput:
id: ti_password
multiline: False
size_hint_y: None
height: 40
font_size: 25

Button:
id: btn_login
text: 'Login!'
height: 100
width: 150
on_press:
root.check_input()

<HomeScreen>
BoxLayout:
orientation: 'vertical'
Label:
text: root.email_address

Label:
text: root.password

最佳答案

根据评论部分的要求,我创建了一个在不使用 kv lang 的情况下在小部件之间绑定(bind)属性的示例。请记住,这可能不是实现目标的最佳方式,因为在这里使用 kv lang 会更容易。

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import mainthread
from kivy.lang import Builder

gui = '''
MyScreenManager

HomeScreen
name: 'home'

LoginScreen
name: 'login'


<HomeScreen>
nickname_input: nickname_input
nickname: nickname_input.text

BoxLayout:
TextInput
id: nickname_input
Button
on_press: root.manager.current = 'login'

<LoginScreen>

BoxLayout:
Label
text: root.nickname
Button
on_press: root.manager.current = 'home'
'''


class HomeScreen(Screen):
nickname_input = ObjectProperty()
nickname = StringProperty()


class LoginScreen(Screen):
nickname = StringProperty()


class MyScreenManager(ScreenManager):

def __init__(self, *args, **kwargs):
super(MyScreenManager, self).__init__(*args, **kwargs)

@mainthread
def delayed():
home_screen = self.get_screen('home')
login_screen = self.get_screen('login')

home_screen.bind(nickname=login_screen.setter('nickname'))
delayed()


class Test(App):

def build(self):
return Builder.load_string(gui)

Test().run()

关于android - 如何使用 kivy StringProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40001239/

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