我想实现一个kivy应用程序,它有两个屏幕(由屏幕管理器管理)。在第一个屏幕(称为LoginScreen)上有两个TextInput字段和一个按钮。在第二个屏幕上,我有两个标签,我想显示在第一个屏幕上输入的值。按钮点击后更改屏幕。
我设法将这些字段绑定在一起,以便在第二个屏幕上显示值。但是,我想在第二个屏幕中“处理”这些值,遗憾的是,我无法做到这一点。
通过“进程”我的意思是,我想使用自定义类(正在运行)登录我的电子邮件帐户并列出我看不见的电子邮件(由其中一个类提供的功能)在一个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
答案 0 :(得分:2)
根据评论部分的要求,我创建了一个在不使用kv lang的情况下在小部件之间绑定属性的示例。请记住,这可能不是实现目标的最佳方式,因为在这里使用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()