Ncurses is a dynamic library that provides text-based terminal window functionality. The Ncurses can:

  • You can use the whole screen
  • Create and manage a window
  • Use 8 different colors
  • Provide mouse support for your programs
  • Use the function keys on your keyboard

Ncurses can run on any ANSI/POSIX compliant Unix/Linux system. In addition, Ncurses can detect terminal properties from the system database, and automatically adjust, providing a terminal-free interface. Therefore,Ncurses works well on different system platforms and different terminals.

The MC toolset is a good example written in Ncurses, and the system core configuration interface on the terminal is also written in Ncurses. Here’s a screenshot of them:

Of course, in this article we are not going to teach you how to write NCurses programs. We are just going to show you how to write NCurses programs in Python.

Before we do that, let’s briefly review some simple syntax for how to use Python.

Let’s start with the simplest Python program:

1
2
3
4
5
6
7
print
"How easy is this?"
x
=
1
y
=
2
z
=
x
+
y
print
"Result of x + y is"
, z

The program is very simple, and I won’t go into details. Save this file as test.py and call Python test.py on the command line to see the output.

Call waiting welfare

1. Recently sorted out 20G resources, including product/operation/test/programmer/market, etc., and Internet practitioners [necessary skills for work, professional books on the industry, precious books on interview questions, etc.]. Access:

  • Scan the code of wechat to follow the public account “Atypical Internet”, forward the article to the moments of friends, and send the screenshots to the background of the public account to obtain dry goods resources links;

2. Internet Communication Group:

  • Pay attention to the public account “atypical Internet”, in the background of the public account reply “into the group”, network sharing, communication;


Let’s look at one more Python function definition — again, very simple, and I don’t need to say much more.

1
2
3
4
5
def
saystuff(mystring):

print
"You said:"
, mystring
saystuff(
"Bach rules"
)
saystuff(
"So does Telemann"
)

So, without further ado, let’s look at how to use NCurses in Python. Here’s a little routine:

1
2
3
4
5
6
7
8
9
10
import
curses
myscreen
=
curses.initscr()
myscreen.border(
0
)
myscreen.addstr(
12
.
25
.
"Python curses in action!"
)
myscreen.refresh()
myscreen.getch()
curses.endwin()

Notice that the first line in this example imports curses, indicating the use of the curses library, and that the display prints “Python curses in action!” in the middle of the screen. Note that under the character interface, 80 x 25 is the screen size, which uses characters, not pixels. Here is a screenshot after it runs:

Finally, let’s look at an example of a digital menu:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env python
from
os
import
system
import
curses
def
get_param(prompt_string):

screen.clear()

screen.border(
0
)

screen.addstr(
2
.
2
, prompt_string)

screen.refresh()

input
=
screen.getstr(
10
.
10
.
60
)

return
input
def
execute_cmd(cmd_string):

system(
"clear"
)

a
=
system(cmd_string)

print
""

if
a
=
=
0
:

print
"Command executed correctly"

else
:

print
"Command terminated with error"

raw_input
(
"Press enter"
)

print
""
x
=
0
while
x !
=
ord
(
'4'
) :

screen
=
curses.initscr()

screen.clear()

screen.border(
0
)

screen.addstr(
2
.
2
.
"Please enter a number..."
)

screen.addstr(
4
.
4
.
"1 - Add a user"
)

screen.addstr(
5
.
4
.
"2 - Restart Apache"
)

screen.addstr(
6
.
4
.
"3 - Show disk space"
)

screen.addstr(
7
.
4
.
"4 - Exit"
)

screen.refresh()

x
=
screen.getch()

if
x
=
=
ord
(
'1'
) :

username
=
get_param(
"Enter the username"
)

homedir
=
get_param(
"Enter the home directory, eg /home/nate"
)

groups
=
get_param(
"Enter comma-separated groups, eg adm,dialout,cdrom"
)

shell
=
get_param(
"Enter the shell, eg /bin/bash:"
)

curses.endwin()

execute_cmd(
"useradd -d "
+
homedir
+
" -g 1000 -G "
+
groups
+
" -m -s "
+
shell
+
""
+
username)

if
x
=
=
ord
(
'2'
) :

curses.endwin()

execute_cmd(
"apachectl restart"
)

if
x
=
=
ord
(
'3'
) :

curses.endwin()

execute_cmd(
"df -h"
)
curses.endwin()

Here’s what it looks like at runtime:

If you know anything about NCurses Programming, you can check out the Related Linux How-to article, linked here: Linux Documentation Project’s NCurses Programming HOW TO