npyscreen简单应用-helloworld(一)

1 啥是npyscreen

npyscreen是一套(基于python标准库curses的)用于编写控制台应用程序的部件库,文档地址。使用npyscreen可以让你的程序在控制台上做出科技感(装x范)十足的操作界面。
npyscreen主要特性由python3编写的,但同时兼容python2(2.6以上版本)。它仅依赖于python的标准库curses,并且有很好的跨平台特性,在windows系统上也能够良好的运行。(相比于 urwid 无法在windows上运行,这点也能算是个优点吧。。。)

2 安装npyscreen

linux:

pip install npyscreen

window:

pip install npyscreen

windows系统使用此方式安装npyscreen,运行程序时,会报错ModuleNotFoundError: No module named '_curses',这个问题的根源就在于curses不支持windows系统orz。解决办法就是使用非官方的whl包安装curses。

whl下载地址

下载对应版本的whl后, 使用控制条执行pip install curses-2.2-cp36-cp36m-win32.whl即可。

在运行npyscreen程序时, 如果报错Python curses Redirection is not supported,可能是因为你使用了non-terminal运行程序(如IDE:PyCharm等,几乎所有的IDE里都是non-terminal),请使用系统原生的控制台通过python *.py执行程序。

3 运行范例

import npyscreen
class TestApp(npyscreen.NPSApp):
    def main(self):
        # These lines create the form and populate it with widgets.
        # A fairly complex screen in only 8 or so lines of code - a line for each control.
        F  = npyscreen.Form(name = "Welcome to Npyscreen",)
        t  = F.add(npyscreen.TitleText, name = "Text:",)
        fn = F.add(npyscreen.TitleFilename, name = "Filename:")
        fn2 = F.add(npyscreen.TitleFilenameCombo, name="Filename2:")
        dt = F.add(npyscreen.TitleDateCombo, name = "Date:")
        s  = F.add(npyscreen.TitleSlider, out_of=12, name = "Slider")
        ml = F.add(npyscreen.MultiLineEdit,
               value = """try typing here!\nMutiline text, press ^R to reformat.\n""",
               max_height=5, rely=9)
        ms = F.add(npyscreen.TitleSelectOne, max_height=4, value = [1,], name="Pick One",
                values = ["Option1","Option2","Option3"], scroll_exit=True)
        ms2= F.add(npyscreen.TitleMultiSelect, max_height =-2, value = [1,], name="Pick Several",
                values = ["Option1","Option2","Option3"], scroll_exit=True)

        # This lets the user interact with the Form.
        F.edit()

        print(ms.get_selected_objects())

if __name__ == "__main__":
    App = TestApp()
    App.run()

运行后就能在控制台中看到程序操作界面了


image

你可能感兴趣的:(npyscreen简单应用-helloworld(一))