我用谷歌搜索,阅读文档(我有点难以理解),用谷歌搜索更多,查看示例,但我找不到答案。也许没有办法做到这一点。
self.button = self.add(npyscreen.Button, name="Button")
这会产生一个按钮,但它似乎更像是一个 True 或 False 选择器。可以用它在新闻上做一些事情,比如启动另一种形式吗?
下面是我为开始使用而整理的一个工作示例应用程序。你能告诉我如何让那个按钮启动第二种形式吗?如果不能通过按钮完成,是否有另一种方法可以本质上做同样的事情?
最终,我希望第一个菜单显示一个选项列表,在选择其中一个选项时,会打开第二个表单。当以第二种形式完成时,它们将被引导回主菜单以根据需要选择更多选项或退出应用程序。除了这个,我想我已经完成了所有我需要的工作。谢谢你的帮助!
#!/usr/bin/python
# encoding=utf8
import npyscreen
# This is a form object
class WizardFormMain(npyscreen.ActionForm, npyscreen.SplitForm, npyscreen.FormWithMenus):
def create(self):
self.button = self.add(npyscreen.Button, name="Button") # Make this button go to FORM2
# Since we are inheriting the npyscreen.FormWithMenus class we can use menus, this will add an option to the menu to exit the program
self.menu = self.new_menu(name="Main Menu", shortcut='^M')
self.menu.addItem("Exit Program", self.exit, "^X")
# END DEF
def exit(self):
self.parentApp.switchForm(None) # causes the app to exit on OK
# END DEF
# Save data to conf file and Go back to first form...
def on_ok(self):
npyscreen.notify_confirm("OK Pressed, going to FORM2 now.", title="Notice", wrap=True, wide=True, editw=1)
self.parentApp.setNextForm('FORM2')
# END DEF
def on_cancel(self):
self.parentApp.setNextForm(None) # Also exit's the program
# END DEF
# END CLASS
# FORM2
class WizardForm2(npyscreen.ActionForm, npyscreen.SplitForm, npyscreen.FormWithMenus):
def create(self):
self.name = self.add( npyscreen.TitleText, name="Username: " )
self.passwd = self.add( npyscreen.TitleText, name="Password: " )
# Save data to conf file and Go back to first form...
def on_ok(self):
npyscreen.notify_confirm("Saved! Going back to main form", title="OK Presed", wrap=True, wide=True, editw=1)
self.parentApp.setNextForm('MAIN')
# END DEF
def on_cancel(self):
npyscreen.notify_confirm("NOT Saved, going back to main form", title="OK Presed", wrap=True, wide=True, editw=1)
self.parentApp.setNextForm('MAIN') # Back to main form
# END DEF
# END CLASS
# This is the Wizards form manager function
class WizardApp(npyscreen.NPSAppManaged):
def onStart(self):
self.addForm('MAIN', WizardFormMain, name = "First Form!", lines=20, columns=60, draw_line_at=16 )
self.addForm('FORM2', WizardForm2, name = "Second Form!", lines=20, columns=60, draw_line_at=16 )
# END DEF
# END CLASS
if ( __name__ == "__main__"):
wizard = WizardApp().run()