Python Curses
17906 ワード
文法入門
cursesアプリケーションのオンとオフ
import curses stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
curses.nocbreak()# ( )
stdscr.keypad(0)
curses.echo() #
curses.endwin()
新しいウィンドウとpadを開く
begin_x = 20;
begin_y = 7;
height = 5;
width = 40;
win = curses.newwin(height, width, begin_y, begin_x)
pad = curses.newpad(100, 100)
# These loops fill the pad with letters; this is# explained in the next sectionfor y in range(0, 100):
for x in range(0, 100):
try:
pad.addch(y,x, ord('a') + (x*x+y*y) % 26)
except curses.error:
pass# Displays a section of the pad in the middle of the screenpad.refresh(0,0, 5,5, 20,75)
テキストの表示
Form Description
str or ch Display the string str or character ch at the current position
str or ch, attr Display the string str or character ch, using attribute attr at the current position
y, x, str or ch Move to position y,x within the window, and display str or ch
y, x, str or ch, attr Move to position y,x within the window, and display str or ch, using attribute attr
属性と色
Attribute Description
A_BLINK Blinking text
A_BOLD Extra bright or bold text
A_DIM Half bright text
A_REVERSE Reverse-video text
A_STANDOUT The best highlighting mode available
A_UNDERLINE Underlined text
stdscr.addstr(0, 0, "Current mode: Typing mode", curses.A_REVERSE)
stdscr.refresh()
stdscr.addstr("Pretty text", curses.color_pair(1))
stdscr.refresh()
ユーザー入力
while 1:
c = stdscr.getch()
if c == ord('p'):
PrintDocument()
elif c == ord('q'):
break# Exit the while()elif c == curses.KEY_HOME:
x = y = 0
例
#-*- coding: UTF-8 -*-
import curses
stdscr = curses.initscr()
def display_info(str, x, y, colorpair=2):
''''' colorpair '''global stdscr
stdscr.addstr(y, x,str, curses.color_pair(colorpair))
stdscr.refresh()
def get_ch_and_continue():
''''' press any key to continue'''global stdscr
# nodelay, 0 stdscr.nodelay(0)
# ch=stdscr.getch()
# nodelay, , 1 stdscr.nodelay(1)
return True
def set_win():
''''' '''global stdscr
# curses.start_color()
# , color pair, 1 2 curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
# curses.noecho()
# curses.cbreak()
# nodelay, , 1 stdscr.nodelay(1)
def unset_win():
''' '''global stdstr
# ( , , ) curses.nocbreak()
stdscr.keypad(0)
curses.echo()
# curses.endwin()
if__name__=='__main__':
try:
set_win()
display_info('Hola, curses!',0,5)
display_info('Press any key to continue...',0,10)
get_ch_and_continue()
except Exception,e:
raise e
finally:
unset_win()
列を間違える
[root@yl-web-test srv]# python curses.pyTraceback (most recent call last):
File "curses.py", line 2, in <module>
import curses
File "/srv/curses.py", line 4, in <module> stdscr = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
小例
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
#!/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()