我已经用Google搜索,阅读了文档(有点难以理解),用Google搜索了更多内容,看了一些示例,但我找不到答案。也许没有办法。
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()
答案 0 :(得分:0)
所以我想我已经找到解决方法。基本上没有有关button方法的文档,所以我通过检查button方法的输出来弄清楚该怎么做。
首先在按钮上,我们可以使用self.add()语句中的value_changed_callback = self.buttonPress指定一个回调函数(类似于javascript)。
然后在示例中的buttonPress函数内部显示一个通知,然后移至FORM2-通过使用以下形式指定表单来完成新表单的移动:self.parentApp.switchForm('FORM2')
这是重要的东西:
class WizardFormMain(npyscreen.ActionForm, npyscreen.SplitForm, npyscreen.FormWithMenus):
def create(self):
self.button = self.add(npyscreen.Button, name="Button", value_changed_callback=self.buttonPress) # 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 buttonPress(self, widget):
npyscreen.notify_confirm("BUTTON PRESSED!", title="Woot!", wrap=True, wide=True, editw=1)
self.parentApp.switchForm('FORM2')
答案 1 :(得分:0)
我花了一些时间才弄清楚,但是在后面的代码中有一个名为ButtonPress的类,然后您可以添加回调函数以使其工作。祝你好运!。
import npyscreen as nps
class Login(nps.ActionForm):
def create(self):
self.usuario = self.add(nps.TitleText, name='Usuario:')
self.contrasena = self.add(nps.TitlePassword, name='Contraseña:',)
def on_ok(self):
self.parentApp.change_form('PERFIL')
class Perfil(nps.ActionForm):
def create(self):
self.add(nps.TitleFixedText, name="Usuario <nombre del usuario>\n", value="")
self.add(nps.ButtonPress, name="Cerrar sesión", when_pressed_function=self.close_session)
def close_session(self):
self.parentApp.change_form('MAIN')
class ElBrutoApp(nps.NPSAppManaged):
def onStart(self):
self.addForm('MAIN',Login, name='El Bruto - Login')
self.addForm('PERFIL',Perfil,name="El Bruto - Perfil")
def change_form(self, name):
self.switchForm(name)
self.resetHistory()
if __name__ == "__main__":
app = ElBrutoApp()
app.run()