2016-07-24 29 views
0

我正在用kivy製作應用程序,而在我的kv文件中,我有一個名爲MainScreen的屏幕,它有兩個TextInput框。當我按下按鈕時,屏幕將變爲LoggedInScreen。如何將輸入從MainScreen上的TextInput框傳遞到LoggedInScreen,以便我可以將它們用作我在main.py中定義的函數的位置參數?這是我試過的(相關位代碼):在kivy中使用另一個屏幕上的變量

<LoggedInScreen>: 
    name: 'loggedin' 
    FloatLayout: 
     TextInput: 

      #This is not working 
      text: root.auth(MainScreen.ids.username.text, MainScreen.ids.password.txt) 

      font_name: 'PCBius.ttf' 
      background_color: (73/255,73/255,73/255,1) 
      keyboard_mode: 'managed' 
      readonly: True 
      size: 405, self.height 
      pos: 495,0 
<MainScreen>: 
    name:'main' 
    FloatLayout: 
     TextInput: 
      id: username 
      multiline: False 
      size_hint: self.width/self.width/5, self.height/self.height/16 
      pos: 200,292 
     TextInput: 
      id: password 
      password: True 
      multiline: False 
      size_hint: self.width/self.width/5, self.height/self.height/16 
      pos: 200,232 

但它輸出一個錯誤,指出沒有定義MainScreen。

感謝您的幫助!

回答

0

最簡單的方法是定義在屏幕經理叫auth_success的新方法,這是負責這兩個屏幕:

class MyScreenManager(ScreenManager): 

    ... 

    def auth_success(self, username, password): 
     self.loggedin.username = username 
     self.loggedin.password = password 
     self.current = 'loggedin' 

現在,當用戶在主屏幕上驗證成功,請致電self.parent.auth_success(self.ids.username.text, self.ids.username.password)

+0

我不認爲你明白我的意思,除非我不明白(這可能是這種情況,我是相當新的)。當我打電話給「MainScreen.ids.username.text」時,它不起作用。那麼,在LoggedInScreen中,如何獲得MainScreen上的一個TextInput框的文本? –

+0

@LoganDarby我明白你的意思。你想在屏幕之間傳遞數據,我給你一個簡單的解決方案。使用屏幕管理器是一種很好的做法,因爲它已經管理屏幕。 'MainScreen.ids.username.text'不起作用,因爲你正在訪問一個類,而不是一個對象。對象作爲子部件存儲在屏幕管理器中。 – jligeza

相關問題