Python npyscreen: How to launch form from button press?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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()
python-2.7 npyscreen
add a comment |
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()
python-2.7 npyscreen
add a comment |
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()
python-2.7 npyscreen
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()
python-2.7 npyscreen
python-2.7 npyscreen
edited Jan 4 at 9:54
John
asked Jan 4 at 9:19
JohnJohn
497512
497512
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54036046%2fpython-npyscreen-how-to-launch-form-from-button-press%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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')
add a comment |
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')
add a comment |
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')
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')
answered Jan 8 at 3:53
JohnJohn
497512
497512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54036046%2fpython-npyscreen-how-to-launch-form-from-button-press%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown