0

我用谷歌搜索,阅读文档(我有点难以理解),用谷歌搜索更多,查看示例,但我找不到答案。也许没有办法做到这一点。

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()

主窗体确定  主表格     主窗体菜单 第二种形式

4

2 回答 2

0

所以我想我已经想出了如何做到这一点。关于按钮方法的文档基本上不存在,所以我通过检查按钮方法输出找出了如何做到这一点。

首先在按钮上,我们可以在 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')
于 2019-01-08T03:53:26.103 回答
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()
于 2020-07-04T02:27:51.240 回答