Displaying Brief Messages and Choices

The following functions allow you to display a brief message or choice to the user.

Notify and related methods are implemented in npyscreen/utilNotify.py

The examples on this page build from this basic program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import npyscreen


class NotifyBaseExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add(npyscreen.FixedText, value=what_to_display)

    def exit_application(self):
        self.parentApp.setNextForm(None)
        self.editing = False


class MyApplication(npyscreen.NPSAppManaged):
    def onStart(self):
        self.addForm('MAIN', NotifyBaseExample, name='To be improved upon')


if __name__ == '__main__':
    TestApp = MyApplication().run()
notify(message, title="Message", form_color='STANDOUT', wrap=True, wide=False)

This function displays a message on the screen. It does not block and the user cannot interact with it - use it to display messages like “Please Wait” while other things are happening.

../examples/notify/notify.py snippet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import npyscreen
import time


class NotifyExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add_handlers({key_of_choice: self.spawn_notify_popup})
        self.add(npyscreen.FixedText, value=what_to_display)

    def spawn_notify_popup(self, code_of_key_pressed):
        message_to_display = 'I popped up \n passed: {}'.format(code_of_key_pressed)
        npyscreen.notify(message_to_display, title='Popup Title')
        time.sleep(1) # needed to have it show up for a visible amount of time
notify_wait(message, title="Message", form_color='STANDOUT', wrap=True, wide=False)

This function displays a message on the screen, and blocks for a brief amount of time. The user cannot interact with it.

../examples/notify/notify_wait.py snippet
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class NotifyWaitExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add_handlers({key_of_choice: self.spawn_notify_popup})
        self.add(npyscreen.FixedText, value=what_to_display)

    def spawn_notify_popup(self, code_of_key_pressed):
        message_to_display = 'I popped up \n passed: {}'.format(code_of_key_pressed)
        npyscreen.notify_wait(message_to_display, title='Popup Title')
notify_confirm(message, title="Message", form_color='STANDOUT', wrap=True, wide=False, editw=0)

Display a message and an OK button. The user can scroll the message if needed. editw controls which widget is selected when the dialog is first displayed; set to 1 to have the OK button active immediately.

../examples/notify/notify_confirm.py snippet
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class NotifyConfirmExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.add_handlers({key_of_choice: self.spawn_notify_popup})
        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add(npyscreen.FixedText, value=what_to_display)

    def spawn_notify_popup(self, code_of_key_pressed):
        message_to_display = 'You need to confirm me, so hit TAB, then ENTER'
        npyscreen.notify_confirm(message_to_display, title= 'popup')
notify_ok_cancel(message, title="Message", form_color='STANDOUT', wrap=True, editw = 0)

Display a message and return True if the user selected ‘OK’ and False if the user selected ‘Cancel’.

../examples/notify/notify_ok_cancel.py snippet
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class NotifyOkCancelExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.add_handlers({key_of_choice: self.spawn_notify_popup})
        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add(npyscreen.FixedText, value=what_to_display)

    def spawn_notify_popup(self, code_of_key_pressed):
        message_to_display = 'You have a choice, to Cancel and return false, or Ok and return true.'
        notify_result = npyscreen.notify_ok_cancel(message_to_display, title= 'popup')
        npyscreen.notify_wait('That returned: {}'.format(notify_result), title= 'results')
notify_yes_no(message, title="Message", form_color='STANDOUT', wrap=True, editw = 0)

Similar to notify_ok_cancel except the names of the buttons are ‘Yes’ and ‘No’. Returns True or False.

selectFile(select_dir=False, must_exist=False, confirm_if_exists=True, sort_by_extension=True)

Display a dialog box for the user to select a filename. Uses the called from directory as the initial folder. The return value is the name of the file selected.

Warning: This form is currently experimental.

../examples/notify/select_file.py snippet
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class SelectFileExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'p'
        what_to_display = 'Press {} for popup \n Press escape key to quit'.format(key_of_choice)

        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add_handlers({key_of_choice: self.spawn_file_dialog})
        self.add(npyscreen.FixedText, value=what_to_display)

    def spawn_file_dialog(self, code_of_key_pressed):
        the_selected_file = npyscreen.selectFile()
        npyscreen.notify_wait('That returned: {}'.format(the_selected_file), title= 'results')

Blanking the Screen

blank_terminal()

This function blanks the terminal. It may sometimes be needed if Forms are being displayed that do not fill the whole screen.

../examples/notify/blank_terminal.py snippet
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import npyscreen
import time


class BlankTerminalExample(npyscreen.Form):
    def create(self):
        key_of_choice = 'b'
        what_to_display = 'Press {} to blank screen \n Press escape key to quit'.format(key_of_choice)

        self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exit_application
        self.add_handlers({key_of_choice: self.initiate_blanking_sequence})
        self.add(npyscreen.FixedText, value=what_to_display)

    def initiate_blanking_sequence(self, code_of_key_pressed):
        npyscreen.blank_terminal()
        time.sleep(1.5)
        npyscreen.notify('..and we\'re back', title='Phew')
        time.sleep(0.75)

    def exit_application(self):
        self.parentApp.setNextForm(None)
        self.editing = False


class MyApplication(npyscreen.NPSAppManaged):
    def onStart(self):
        self.addForm('MAIN', BlankTerminalExample, name='To show off blank_screen')


if __name__ == '__main__':
    TestApp = MyApplication().run()