2016-10-12 66 views
2

我想實現一個kivy應用程序,它有兩個屏幕(由屏幕管理器管理)。在第一個屏幕(稱爲LoginScreen)上有兩個TextInput字段和一個按鈕。在第二個屏幕上,我有兩個標籤,我想要顯示第一個屏幕上輸入的值。點擊按鈕後,更改屏幕。
我設法將這些字段綁定在一起,以便值顯示在第二個屏幕上。但是,我想在第二個屏幕上「處理」這些值,但不幸的是,我無法做到這一點。
「process」我的意思是說,我想用一個定製的類(它正在工作)登錄到我的電子郵件帳戶,並列出我看不到的電子郵件(它是由一個類的函數提供的) kivy名單。
可以,有人請告訴我如何獲得輸入的值並使用它們創建我的課程?
我將不勝感激任何幫助!
我.py文件:如何使用kivy StringProperty?

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

你的問題是關於使用字符串屬性,還是在屏幕之間傳遞數據?因爲如果第二個,這裏有很多例子,你可以很容易地找到很少的例子。 – jligeza

+0

感謝您的回覆。我添加了源代碼。如果我嘗試從變量email_addess和password中獲取值,例如在HomeScreen的init方法中,我不能(我想將它們打印出來)。在我在check_input方法中註釋的部分中,我可以訪問HomeScreen的屬性值。 – Bence

+0

檢查此主題:[鏈接](http://stackoverflow.com/questions/39807997/how-to-access-some-widget-attribute-from-another-widget-in-kivy/39808691#39808691) – jligeza

回答

1

正如在評論部分的要求,我創建的結合部件之間的屬性,而無需使用KV郎的例子。請記住,這可能不是實現目標的最佳方式,因爲在這裏使用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() 
+0

非常感謝您的回答! – Bence