在命令行中创建用户界面
在linux和mac上,这个库是默认安装的。在Windows上可以安装windows-curses
关联工程:https://github.com/GZhonghui/AnnotationSync
示例代码(AI):
import curses
def main(stdscr):
# 禁用光标显示
curses.curs_set(0)
# 定义选项
options = ["Option 1", "Option 2", "Option 3", "Exit"]
current_row = 0
# 定义一个函数来绘制当前的选项列表
def print_menu(current_row):
stdscr.clear()
h, w = stdscr.getmaxyx()
for idx, row in enumerate(options):
x = w//2 - len(row)//2
y = h//2 - len(options)//2 + idx
if idx == current_row:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
# 设置颜色对
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
# 主循环
while True:
print_menu(current_row)
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(options) - 1:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
if options[current_row] == "Exit":
break
stdscr.clear()
stdscr.addstr(0, 0, f"You selected '{options[current_row]}'")
stdscr.refresh()
stdscr.getch()
# 清除屏幕
stdscr.clear()
# 运行 curses 应用
curses.wrapper(main)