Home >Backend Development >Python Tutorial >Python text terminal GUI framework, so cool
The first one to appear is Curses[1].
Curse
Curses is a dynamic library that provides text-based terminal window functions. It can:
Curses can run on any Unix/Linux system that follows the ANSI/POSIX standard. It can also run on Windows, but you need to install the windows-curses library additionally:
pip install windows-curses
The picture above is a Tetris game written by a buddy using Curses[2]. It doesn’t feel full of memories, so you can take it Go and resurrect antique machines.
Let’s try our best:
import curses myscreen = curses.initscr() myscreen.border(0) myscreen.addstr(12, 25, "Python curses in action!") myscreen.refresh() myscreen.getch() curses.endwin()
The running effect of the code is as follows:
##a small test Curses is very lightweight, especially suitable for handling simple interactions and replacing complex parameter inputs The program is both elegant and simple, and Curses is also the basis for other text terminal UIs. NpyscreenNpyscreen[3] is also a Python component library for writing text terminals. It is an application framework built based on Curses. Compared to Curses, Npyscreen is closer to UI programming. UI display and interaction are completed through the combination of components, and Npyscreen can adapt to screen changes. Npyscreen provides multiple controls, such as form (Form), single-line text input box (TitleText), date control (TitleDateCombo), multi-line text input box (MultiLineEdit), radio selection list (TitleSelectOne), and progress Bar (TitleSlider) and other controls. Provides powerful functions to meet the requirements for rapid program development, whether it is a simple single-page program or a complex multi-page application. Let’s take a look at a small example: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()
色彩
不知道你看了是什么感觉,我的感觉是:这也太卷了吧~
几乎可以做 GUI 下的所有事情!
更厉害的是,Urwid 完全是按照面向对象的思想打造的框架:
Urwid 结构图
现在我们来小试一把,感受一下 Urwid 的强大:
import urwid def show_or_exit(key): if key in ('q', 'Q'): raise urwid.ExitMainLoop() txt.set_text(repr(key)) txt = urwid.Text(u"Hello World") fill = urwid.Filler(txt, 'middle') loop = urwid.MainLoop(fill, unhandled_input=show_or_exit) loop.run()
运行这段代码,就可以看到命令行被设置为交互模式,按键时会在窗口中央显示出键名,如果按下 q 键,程序就会退出。
注意:
Urwid 只能在 Linux 操作系统中运行,Windows 上会因为缺失必要组件无法运行
限于篇幅,这里只展示了三种文本终端框架,不过已经能对基于文本终端 UI 框架的强大感受一二了。
还有一些框架也很优秀,比如 prompt_toolkit,有兴趣的同学可以研究一下。
虽然基于文本终端的 UI 早已不是主流,但是在一些特殊的行业或者业务中,还是有其存在的价值,研究一下,说不定在特殊的地方可以帮助到我们。
最后,推荐一个很有意思的基于文本终端的应用 —— 命令行网易云音乐[9]:
NetEase-MusicBox
是基于 Curses 开发,如果运行起来,能被它的强悍所震撼,有空可以玩玩,比心!
The above is the detailed content of Python text terminal GUI framework, so cool. For more information, please follow other related articles on the PHP Chinese website!