0

I've googled, read the docs (kinda hard for me to understand), googled some more, looked at examples, and I just can't find an answer. Maybe there is no way to do it.

self.button = self.add(npyscreen.Button, name="Button")

This produces a button but it seems to be more of a True or False selector. Can one do something on-press with it, like launch another form?

Below is a working example application I put together to get started. Can you show me how to make that button launch the second form? If it can't be done with a button, is there another way to essentially do the same thing?

Ultimately I would like the first menu to display a list of options that upon selecting one of them, would open up a second form. When done in the second form, they would be directed back to the main menu to select further options as needed or exit the application. I think I have everything I need worked out except this. Thanks for your help!

#!/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()

MAIN form OK  Main Form     MAIN form menu SECOND form

Community
  • 1
  • 1
John
  • 976
  • 1
  • 15
  • 21

2 Answers2

0

So I think I have figured out how to do this. The docs regarding the button method is basically absent, so I figured out how to do this by examining the button method output.

First on the button we can specify a callback function (similar to javascript) with value_changed_callback=self.buttonPress inside of the self.add() statement.

Then inside of the buttonPress function in the example shows a notification and then moves over to FORM2 - moving to the new form is done by specifying the form with this: self.parentApp.switchForm('FORM2')

Here it is the important stuff:

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')
John
  • 976
  • 1
  • 15
  • 21
0

It took me a while to figure it out, but looking the code behind there is a class called ButtonPress, and then you can add the callback function to make it work. Good luck!.

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