加载中…
个人资料
Hope
Hope
  • 博客等级:
  • 博客积分:0
  • 博客访问:991,058
  • 关注人气:295
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Pythoncurses通过移动键盘上下键选中某一列

(2019-01-30 09:19:38)
标签:

python

curses

it

分类: Python

第一:实现上下键捕捉


#handle key up,down, right, left
if
input_val == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif
input_val == curses.KEY_UP:
cursor_y = cursor_y - 1
elif
input_val == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif
input_val == curses.KEY_LEFT:
cursor_x = cursor_x - 1

height, width = screen.getmaxyx()
cursor_x = max(0, cursor_x)
cursor_x = min(width - 1, cursor_x)

cursor_y = max(0, cursor_y)
cursor_y = min(height - 1, cursor_y)

第二:移动到某一列制造选中效果


#制造选中效果
if cursor_y == 4:
screen.addstr(4, 4, "1 - 集群配置", curses.A_STANDOUT)
elif cursor_y == 5:
screen.addstr(5, 4, "2 - 资源信息", curses.A_STANDOUT)
elif cursor_y == 6:
screen.addstr(6, 4, "3 - 数据库信息", curses.A_STANDOUT)
elif cursor_y == 7:
screen.addstr(7, 4, "4 - 安装概要", curses.A_STANDOUT)
elif cursor_y == 8:
screen.addstr(8, 4, "5 - 退出", curses.A_STANDOUT)
else:
screen.addstr(4, 4, "1 - 集群配置", curses.A_STANDOUT)
#制造选中效果结束

screen.refresh()

实现效果图如下:

Pythoncurses通过移动键盘上下键选中某一列

完整可运行代码如下:


#!/usr/bin/env python

from os import system
import curses

#from curses.textpad import Textbox, rectangle

#-- Define default conversion dictionary
cfg_dict = {'network': 'enp5s0f1',
'subnet'
: '192.168.100.0',
'type'
: 'public'
}

cfg_dict2 = {'network': 'enp3s0f1',
'subnet'
: '10.0.0.1',
'type'
: 'private'
}
counter = 0
cursor_x = 0
cursor_y = 0

def draw_dict():
screen.addstr(5,33, " "*43, curses.A_NORMAL)
screen.addstr(8,33, " "*43, curses.A_NORMAL)
screen.addstr(11,33, " "*43, curses.A_NORMAL)
screen.addstr(14,33, " "*43, curses.A_NORMAL)
screen.addstr(5, 33, cfg_dict['source'], curses.A_STANDOUT)
screen.addstr(8, 33, cfg_dict['target'], curses.A_STANDOUT)
screen.addstr(11,33, cfg_dict['type'], curses.A_STANDOUT)
screen.addstr(14,33, cfg_dict['proxy'], curses.A_STANDOUT)
screen.addstr(17,33, str(counter), curses.A_STANDOUT)
screen.refresh()

def get_param(prompt_string):
screen.clear()
screen.border(0)
screen.addstr(2, 2, prompt_string, curses.A_UNDERLINE)
screen.refresh()
input = screen.getstr(10, 10, 60)
return input

def addStutusBarWin():
# split into 2 parts
height, width = screen.getmaxyx()
subwin = screen.subwin(0, width, 0, 0)
subwin.box()
cliwin = screen.subwin(0, width, height - 5, 0)
cliwin.box()
# Render status bar状态栏信息显示
statusbarstr = "Press '5' to exit | Pos: {}, {}".format(cursor_x, cursor_y)
height, width = screen.getmaxyx()
screen.addstr(height - 3, 2, statusbarstr)
# 状态栏信息显示结束
screen.refresh()


def add_param(prompt_string):
screen.clear()
screen.border(0)
addStutusBarWin()
#------添加一个小窗口----------------
'''editwin = curses.newwin(5, 30, 2, 1)
rectangle(screen, 1, 0, 1 + 5 + 1, 1 + 30 + 1)
screen.refresh()
box = Textbox(editwin)
# Let the user edit until Ctrl-G is struck.
box.edit()
# Get resulting contents
message = box.gather()'''
#------------------------------------

screen.addstr(2, 2, prompt_string)

screen.addstr(5, 4, " 网卡:", curses.A_BOLD)
screen.addstr(6, 4, " 子网:", curses.A_BOLD)
screen.addstr(7, 4, " 类型:", curses.A_BOLD)


screen.addstr(5, 33, " " * 43, curses.A_NORMAL)
screen.addstr(6, 33, " " * 43, curses.A_NORMAL)
screen.addstr(7, 33, " " * 43, curses.A_NORMAL)


screen.addstr(5, 33, cfg_dict['network'], curses.A_DIM)
screen.addstr(6, 33, cfg_dict['subnet'], curses.A_DIM)
screen.addstr(7, 33, cfg_dict['type'], curses.A_DIM)
# screen.refresh()



screen.hline(9, 15, curses.ACS_HLINE, 30)
screen.refresh()
#---------------------------------second line-------------------------
screen.addstr(10, 4, " 网卡:", curses.A_BOLD)
screen.addstr(11, 4, " 子网:", curses.A_BOLD)
screen.addstr(12, 4, " 类型:", curses.A_BOLD)


screen.addstr(10, 33, " " * 43, curses.A_NORMAL)
screen.addstr(11, 33, " " * 43, curses.A_NORMAL)
screen.addstr(12, 33, " " * 43, curses.A_NORMAL)


screen.addstr(10, 33, cfg_dict2['network'], curses.A_DIM)
screen.addstr(11, 33, cfg_dict2['subnet'], curses.A_DIM)
screen.addstr(12, 33, cfg_dict2['type'], curses.A_DIM)


screen.refresh()

cfg_dict['network'] = screen.getstr(5, 33)
cfg_dict['subnet'] = screen.getstr(6, 33)
cfg_dict['type'] = screen.getstr(7, 33)

cfg_dict2['network'] = screen.getstr(10,33)
cfg_dict2['subnet'] = screen.getstr(11,33)
cfg_dict2['type'] = screen.getstr(12,33)



screen.refresh()

input_val = 0
cursor_x = 4
cursor_y = 4

while input_val != ord('5'):
screen = curses.initscr()

curses.echo()
curses.cbreak()
screen.keypad(1)
curses.start_color()
screen.clear()

#modify the font color
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)

addStutusBarWin()

#handle key up,down, right, left
if
input_val == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif
input_val == curses.KEY_UP:
cursor_y = cursor_y - 1
elif
input_val == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif
input_val == curses.KEY_LEFT:
cursor_x = cursor_x - 1

height, width = screen.getmaxyx()
cursor_x = max(0, cursor_x)
cursor_x = min(width - 1, cursor_x)

cursor_y = max(0, cursor_y)
cursor_y = min(height - 1, cursor_y)


screen.border(0)

screen.addstr(2, 2, "请选择配置项:")
screen.addstr(4, 4, "1 - 集群配置")
screen.addstr(5, 4, "2 - 资源信息")
screen.addstr(6, 4, "3 - 数据库信息")
screen.addstr(7, 4, "4 - 安装概要")
screen.addstr(8, 4, "5 - 退出")


height, width = screen.getmaxyx()
#制造选中效果
if cursor_y == 4:
screen.addstr(4, 4, "1 - 集群配置", curses.A_STANDOUT)
elif cursor_y == 5:
screen.addstr(5, 4, "2 - 资源信息", curses.A_STANDOUT)
elif cursor_y == 6:
screen.addstr(6, 4, "3 - 数据库信息", curses.A_STANDOUT)
elif cursor_y == 7:
screen.addstr(7, 4, "4 - 安装概要", curses.A_STANDOUT)
elif cursor_y == 8:
screen.addstr(8, 4, "5 - 退出", curses.A_STANDOUT)
else:
screen.addstr(4, 4, "1 - 集群配置", curses.A_STANDOUT)
#制造选中效果结束

screen.refresh()

input_val = screen.getch()

if input_val == ord('1'):
screen.clear()
screen.border(0)
addStutusBarWin()
screen.addstr(2, 2, "请选择配置项:")
screen.addstr(4, 4, "1 - 网络配置")
screen.addstr(5, 4, "2 - 主节点")
screen.addstr(6, 4, "3 - 备节点")
screen.addstr(7, 4, "4 - 设备互信")
screen.addstr(8, 4, "5 - 返回")

screen.refresh()

y = screen.getch()

if y== ord('1'):
username = add_param("网络配置")
curses.endwin()
if y == ord('2'):
username = get_param("click 2")
curses.endwin()

if y == ord('3'):
username = get_param("click 3")
curses.endwin()
# execute_cmd("df -h")
if
y == ord('4'):
username = get_param("click 4")
curses.endwin()
if y == ord('5'):
curses.endwin()

if input_val == ord('2'):
username = get_param("click 2")
curses.endwin()

if input_val == ord('3'):
username = get_param("click 3")
curses.endwin()

if input_val == ord('4'):
username = get_param("click 4")
curses.endwin()
if input_val == ord('5'):
curses.endwin()

curses.endwin()

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有