Article 2: super simple! Remove watermarks from images and PDF s
Article 3: how to learn Python on Github? Don't miss these popular warehouses!
Sort out several common UI frameworks based on text terminals and have a look!
The first is Curses.
Curves is a dynamic library that can provide the function of text-based terminal window. It can:
- Use the entire screen
- Create and manage a window
- Use 8 different colors
- Provide mouse support for programs
- Use the function keys on the keyboard
Curves 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 friend with curves . I don't feel full of memories. I can take it to revive the antique machine.
Let's also try ox knife:
import curses myscreen = curses.initscr() myscreen.border(0) myscreen.addstr(12, 25, "Python curses in action!") myscreen.refresh() myscreen.getch() curses.endwin()
- Note that the first two parameters of addstr are character coordinates, not pixel coordinates
- getch blocks the program until it waits for keyboard input
- curses.endwin() is used to exit the window
- If you need to continuously monitor user interaction, you need to write a loop and judge the input obtained by getch()
The code runs as follows:
Curves is very lightweight, especially suitable for dealing with simple interaction and replacing complex parameter input procedures. It is elegant and simple, and curves is also the basis of other text terminal UI.
Npyscreen is also a Python component library for writing text terminals. It is an application framework based on curves.
Compared with curves, Npyscreen is closer to UI programming. It completes UI display and interaction through the combination of components, and Npyscreen can adapt to screen changes.
Npyscreen provides multiple controls, such as Form, TitleText, TitleDateCombo, MultiLineEdit, TitleSelectOne, and TitleSlider.
Provide powerful functions to meet the requirements of rapid development of programs, whether simple single page programs or complex multi page applications.
Let's take 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()
- Npyscreen module is introduced. If not, it can be installed through pip: pip install npyscreen
- Inherit npyscreen Npsapp creates an application class TestApp
- Create some properties of the Form object and set a method on the Form object
- Call the Edit method of the form object and give the operation right to the user
- At runtime, instantiate TestAPP, then invoke the run method to start the application, and the application can enter the state of waiting for user interaction.
The effect of the above code is as follows:
- [Tab] / [Shift + Tab] is used to switch control focus
- [Enter] / [space] is used to enter selection, setting and confirmation
- In the selection frame, the direction key is similar to vim, that is, it is controlled by hjkl
Isn't it amazing that so many complex operations can be done with text? Are the doubts about the progress display in the command line clear~
If Curses and Npysreen are lightweight text terminal UI frameworks, Urwid is definitely a heavyweight player.
Urwid includes many features for developing text UI, such as:
- Application window adaptation
- Automatic text alignment
- Easily set up text blocks
- Powerful selection box control
- It can be integrated with various event driven frameworks, such as Twisted, Glib, Tornado, and so on
- It provides a variety of prefabricated controls such as edit box, button, multi (single) selection box and so on
- The display mode supports native, curves mode, LCD display and network display
- Support UTF-8 and CJK character set (Chinese can be displayed)
- Support multiple colors
See the effect:
I don't know how you feel after watching it. My feeling is: it's too curly~
Can do almost everything under the GUI!
What's more, Urwid is completely based on the idea of object-oriented:
Now let's give it a try and feel the power of 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()
- First introduce the urwid module
- Defines an input event handling method, show_or_exit
- In the processing method, when the input key is Q or Q, exit the main cycle, otherwise the key name will be displayed
- urwid.Text is a text control that accepts a string as the display information
- urwid.Filler is similar to panel. It fills the txt control on it and sets the position in the center of the window
- urwid.MainLoop sets the main loop of Urwid, takes fill as the drawing entry of the control, and the parameter is unhandled_input accepts a key event processing method, using the previously defined show_or_exit
- loop.run() starts the UI and monitors various events
Running this code, you can see that the command line is set to interactive mode. When pressing the key, the key name will be displayed in the center of the window. If you press the q key, the program will exit.
Note: Urwid can only run in Linux operating system. Windows cannot run because of missing necessary components
Limited to space, only three text terminal frameworks are shown here, but we can already have a strong feeling about the UI framework based on text terminal.
Some frameworks are also excellent, such as prompt_toolkit, interested students can study it.
Although the UI based on text terminal is no longer the mainstream, it still has its value in some special industries or businesses. Research may help us in special places.
Finally, I recommend an interesting application based on text terminal - command line Netease cloud music :
It is developed based on curves. If it runs, it can be shocked by its strength. If you have time, you can play and compare your heart!