2015-12-19 63 views
1

我想在python中創建一個簡單的npyscreen curses應用程序,它在一個屏幕上請求用戶輸入,然後在另一個屏幕上驗證用戶。在npyscreen之間傳遞窗體之間的值

大多數情況下,這是努力瞭解如何在npyscreen中存儲和檢索值。我確信我缺少一些簡單的東西,但是我一直無法找到(或理解?)文檔中的答案。

下面的代碼示例將無法正常傳遞值:

#!/usr/bin/env python3.5 

import npyscreen as np 

class EmployeeForm(np.Form): 
    def afterEditing(self): 
     self.parentApp.switchForm('CONFIRMFM') 

    def create(self): 
     self.myName = self.add(np.TitleText, name='Name') 
     self.myDepartment = self.add(np.TitleSelectOne, scroll_exit=True, max_height=3, name='Department', values = ['Department 1', 'Department 2', 'Department 3']) 
     self.myDate = self.add(np.TitleDateCombo, name='Date Employed') 

class EmployeeConfirmForm(np.Form): 
    def afterEditing(self): 
     self.parentApp.setNextForm(None) 

    def create(self): 
     self.value = None 
     self.wgName = self.add(np.TitleText, name = "Name:",) 
     self.wgDept = self.add(np.TitleText, name = "Dept:") 
     self.wgEmp  = self.add(np.TitleText, name = "Employed:") 

    def beforeEditing(self): 
     if self.value: 
      self.name = "Is this correct?" 
      self.wgName.value = self.myName.value 
      self.wgDept.value = self.myDepartment.value 
      self.wgEmp.value  = self.myDate.value 

    def on_cancel(self): 
     self.parentApp.switchFormPrevious() 


class myApp(np.NPSAppManaged): 
    def onStart(self): 
     self.addForm('MAIN', EmployeeForm, name='Employee Entry') 
     self.addForm('CONFIRMFM', EmployeeConfirmForm, name='Employee Confirmation') 

if __name__ == '__main__': 
    TestApp = myApp().run() 

回答

1

一種方式做,這是使用getForm switchForm前值傳遞。以下對我使用python2來傳遞名稱。傳遞其他值應該是相似的。我自己非常瞭解這一點,但如果您還有其他問題,請發表評論。

import npyscreen as np 

class EmployeeForm(np.ActionForm): 
    def create(self): 
     self.myName = self.add(np.TitleText, name='Name') 
     self.myDepartment = self.add(np.TitleSelectOne, scroll_exit=True, max_height=3, name='Department', values = ['Department 1', 'Department 2', 'Department 3']) 
     self.myDate = self.add(np.TitleDateCombo, name='Date Employed') 

    def afterEditing(self): 
     self.parentApp.getForm('CONFIRMFM').wgName.value = self.myName.value 
     self.parentApp.switchForm('CONFIRMFM') 

class EmployeeConfirmForm(np.Form): 
    def afterEditing(self): 
     self.parentApp.setNextForm(None) 

    def create(self): 
     self.value = None 
     self.wgName = self.add(np.TitleFixedText, name = "Name:",) 
     self.wgDept = self.add(np.TitleText, name = "Dept:") 
     self.wgEmp  = self.add(np.TitleText, name = "Employed:") 

    def on_cancel(self): 
     self.parentApp.switchFormPrevious() 

class myApp(np.NPSAppManaged): 
    def onStart(self): 
     self.addForm('MAIN', EmployeeForm, name='Employee Entry') 
     self.addForm('CONFIRMFM', EmployeeConfirmForm, name='Employee Confirmation') 

if __name__ == '__main__': 
    TestApp = myApp().run() 
1

我還沒有找到直接在表單之間傳遞變量的直接方法。但是,您可以通過使用表單中具有全局作用域的變量來解決此問題。

NPSAppManaged應用程序類運行完全封裝,所以如果您嘗試在Python文件的頂部聲明全局變量,那麼其中的表單將無法訪問它們。相反,請在onStart方法之前聲明NPSAppManaged應用程序類中的變量,如下所示。

class myApp(np.NPSAppManaged): 

    # declare variables here that have global scope to all your forms 
    myName, myDepartment, myDate = None, None, None 

    def onStart(self): 
     self.addForm('MAIN', EmployeeForm, name='Employee Entry') 
     self.addForm('CONFIRMFM', EmployeeConfirmForm, name='Employee Confirmation') 

if __name__ == '__main__': 
    TestApp = myApp().run() 

然後,您可以訪問使用「self.parentApp [變量名]。」以下這些變量:

class EmployeeConfirmForm(np.Form): 
    def afterEditing(self): 
     self.parentApp.setNextForm(None) 

    def create(self): 
     self.add(np.FixedText, value = "Is this correct?") 
     self.wgName = self.add(np.TitleText, name = "Name:", value = self.parentApp.myName) 
     self.wgDept = self.add(np.TitleText, name = "Dept:", value = self.parentApp.myDepartment) 
     self.wgEmp = self.add(np.TitleText, name = "Employed:", value = self.parentApp.myDate) 

    def on_cancel(self): 
     self.parentApp.switchFormPrevious() 

注意:你不需要有因爲值的獨立beforeEditing方法將在應用程序類全局變量的create方法中直接加載到EmployeeConfirmForm中。