2021-05-21 Basic usage of curses in python

Hits: 0

Sometimes the Linux system does not have an interface, and you may need to complete the program interaction and status display in the terminal terminal. ncurses in C language supports the development of [command-line] interface programs, and curses is a python terminal interface library based on ncurses.

This article implements a simple demo applet, which is convenient for beginners to learn and use.

First go to the demo main program

#-*- coding: UTF-8 -*-
import curses
import locale
locale.setlocale(locale.LC_ALL, '')

def  terminal_display (stdscr) : 
    stdscr.leaveok( 1 ) #Configure the cursor to move with the character update 
    curses.cbreak() #Close the line buffer, disable character processing other than interrupt and character flow control 
    curses.echo() #Enter character echo 
    stdscr.keypad( 1 ) #Enable special keyboard key response 
    curses.start_color() #Enable color function 
    curses.init_pair( 1 , curses.COLOR_GREEN, curses.COLOR_BLACK) #Change color pairing, serial number, foreground color (word color), background color 
    curses.init_pair( 2 , curses.COLOR_YELLOW, curses.COLOR_BLACK)
    curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
    stdscr.clear() #Clear the screen 
    show_lines, line_len = stdscr.getmaxyx() #Get the number of lines displayed on the screen and the number of words per line 
    stdscr.addstr( 0 , 0 , "The number of screen lines: %s, the number of line characters: %s\ n" %(show_lines,line_len))
    stdscr.addstr( 1 , 0 , "Please enter an integer:" )
    stdscr.nodelay( 0 )
    input_num = int(stdscr.getstr())
    stdscr.addstr( 2 , 0 , "The input integer is: %s" % input_num)
    stdscr.nodelay( 1 )
    stdscr.addstr( 3 , 0 , "This line should be in white font" , curses.color_pair( 0 )) #Number 0 is the default configuration and cannot be changed 
    stdscr.addstr( 4 , 0 , "This line should be in green font" , curses.color_pair( 1 ))
    stdscr.addstr( 5 , 0 , "This line should be in yellow font" , curses.color_pair( 2 ))
    stdscr.addstr( 6 , 0 , "This line should be in red font" , curses.color_pair( 3 ))
    while 1:
if __name__ == '__main__':

The result after the call is roughly as follows:

Curses. [wrapper] is a good thing. I didn’t use wrapper at first. After calling curses program, the whole terminal was messed up, carriage return couldn’t wrap, and various keyboard keys couldn’t respond.

I guess the internal function of curses.wrapper(terminal_display) looks like this:

def wrapper(func, ...)
    stdscr = curses.initscr()
    ... ##Record the current configuration of the terminal
    ... ##Restore the terminal to its original state

After the call, the wrapper will first complete the initialization of curses, save the current terminal state, call func and pass the initialized screen handle to func as its first parameter. The third parameter is passed in. I can’t use it here, so I didn’t add parameters.

In addition, the wrapper input parameter supports class functions, because the first parameter of the class function is self by default, I am a little worried, but there is no problem in the actual measurement, you can use it as usual.

import curses

class TestDisplay():
    def __init__():

    def display(self,stdscr):
        stdscr.addstr(0,0,"hello world")
        while 1:

if __name__ == '__main__':
    t = TestDisplay()

If you want to learn and research the curses function in depth, it is recommended to read this. It seems to be an official document. There is little difference between python2 and python3. My environment is python2.

You may also like...

Leave a Reply

Your email address will not be published.