显示简明的消息和选择

下列函数允许你向用户显示一个简短的消息或选择。

通知相关的方法是在 npyscreen/utilNotify.py 中实现的。

本页的例子从这个基本程序上建立的:

 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)

这个函数在屏幕上显示一条消息。它不阻塞,且用户不会与它交互 - 在其他事情正在发生时,使用它来显示类似“Please wait”的消息。

../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)

这个函数在屏幕上显示一条消息,且阻塞很短的一段时间。用户不会与它进行交互。

../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)

显示一条消息和OK按钮。如果需要,用户可以滚动消息。editw控制对话框首次显示时选择哪一个控件;设置为1则立刻激活OK按钮。

../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)

显示一条消息,如果用户选择‘OK’,则返回True;如果用户选择‘Cancel’,则返回False。

../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)

notify_ok_cancel 相似,除了按钮名称是‘Yes’和‘No’,返回True或False。

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

显示一个提示用户选择文件名的对话框。使用来自目录的调用作为初始化文件夹。返回值是所选文件的名称。

Warning: 这个形式当前还是试验阶段。

../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')

空白的屏幕

blank_terminal()

这个函数使终端消失。如果显示的表单没有占满整个屏幕,它有时可能被需要。

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