python + curses 在终端上开发光标菜单

简介:
Netkiller代码   收藏代码
  1. 按ESC键弹出菜单  
Netkiller代码   收藏代码
  1. 资料比较少,这年头基本没有人写curses。  
Netkiller代码   收藏代码
  1. #!/usr/bin/env python3  
  2. from os import system  
  3. import curses, subprocess  
  4.    
  5. def get_param(prompt_string):  
  6.     screen.clear()  
  7.     screen.border(0)  
  8.     screen.addstr(22, prompt_string)  
  9.     screen.refresh()  
  10.     input = screen.getstr(101060)  
  11.     return input  
  12.    
  13. def execute_cmd(cmd_string):  
  14.     system("clear")  
  15.     a = system(cmd_string)  
  16.     print("")  
  17.     if a == 0:  
  18.       print ("Command executed correctly")  
  19.     else:  
  20.       print ("Command terminated with error")  
  21.     raw_input("Press enter")  
  22.     print ("")  
  23.    
  24. blacklist=['']  
  25.   
  26. screen = curses.initscr()  
  27. curses.noecho();   
  28. curses.cbreak()  
  29. screen.keypad(1)  
  30. history = []  
  31. line = 0  
  32. column = 0  
  33. char = []  
  34. key = 0  
  35.   
  36. screen.clear()  
  37. height,width = screen.getmaxyx()  
  38. #print (height,width)  
  39. subwin = screen.subwin(0, width, 00)  
  40. subwin.box()  
  41. cliwin = screen.subwin(0, width, height-30)  
  42. cliwin.box()  
  43.    
  44. def menu(screen):  
  45.     height=0  
  46.     width = 30  
  47.     top=5  
  48.     left=3  
  49.     menuwin =screen.subwin(height , width,top ,left )  
  50.     menuwin.keypad(1)  
  51.     menuwin.border(0)  
  52.     menubar = ["1 - Add a user""2 - Restart Apache""3 - Show disk space""Test""Neo""Netkiller""4 - Exit"]  
  53.     menuwin.addstr(11"Please enter a number...")  
  54.       
  55.     current = 0   
  56.     while 1 :  
  57.         menuitem = 0  
  58.         #print (menuitem,current )  
  59.         menuwin.refresh()  
  60.         for m in menubar:  
  61.             if current == menuitem:  
  62.                 menuwin.addstr(menuitem+24,m , curses.A_REVERSE)  
  63.             else:  
  64.                 menuwin.addstr(menuitem+24, m)  
  65.             menuitem=menuitem+1  
  66.               
  67.         key = menuwin.getch()  
  68.         if key == curses.KEY_UP:  
  69.             if current <= 0 :  
  70.                 current = 0  
  71.             else:  
  72.                 current = current-1  
  73.             #char = history[menuitem]  
  74.             #cliwin.clear()  
  75.             #cliwin.addstr(1,1,char)  
  76.             #print("up",line)  
  77.             #print(history[line])  
  78.         if key == curses.KEY_DOWN:  
  79.             if current >= len(menubar)-1 :  
  80.                 current = len(menubar)-1  
  81.             else:  
  82.                 current = current + 1  
  83.             #char = history[menuitem]  
  84.             #cliwin.clear()  
  85.             #cliwin.addstr(1,1,char)  
  86.             #print("down",line)  
  87.             #print(history[line])  
  88.         if key == 10:  
  89.             choice = current  
  90.             print(choice)  
  91.         #if key == 27:  
  92.         #   return  
  93.   
  94. while key != ord('q'):  
  95.       
  96.     screen.refresh()  
  97.     subwin = screen.subwin(0, width, 00)  
  98.     subwin.box()  
  99.     cliwin = screen.subwin(0, width, height-30)  
  100.     cliwin.box()  
  101.       
  102.   
  103.     key = screen.getch()  
  104.       
  105.     #print(key)  
  106.       
  107.     if 31<key<126:  
  108.         c=chr(key)  
  109.         char.append(c)  
  110.         #screen.addstr(2,2,c)  
  111.         cliwin.addstr(1,column+1,c)  
  112.         column = column+1  
  113.         #screen.refresh()  
  114.     else:   
  115.         pass                  # Ignore incorrect keys  
  116.     if key in (curses.KEY_ENTER,10):  
  117.         if len(char) > 1:  
  118.             cmd = ''.join(char)  
  119.             history.append(cmd)  
  120.             #system(cmd)  
  121.             subwin.clear()  
  122.             subwin.addstr(1,1,subprocess.getoutput(cmd))  
  123.             char = []  
  124.             line += 1  
  125.             column = 0  
  126.             cliwin.refresh()  
  127.             cliwin.clear()  
  128.             #print ("ENTER!!!")  
  129.           
  130.     if key == curses.KEY_LEFT:   
  131.         curses.beep()  
  132.         print("left")  
  133.     if key == curses.KEY_RIGHT:  
  134.         curses.beep()  
  135.         print("right")  
  136.     if key == curses.KEY_UP:  
  137.         if line <= 0 :  
  138.             line = 0  
  139.         else:  
  140.             line = line-1  
  141.         char = history[line]  
  142.         cliwin.clear()  
  143.         cliwin.addstr(1,1,char)  
  144.         #print("up",line)  
  145.         #print(history[line])  
  146.     if key == curses.KEY_DOWN:  
  147.         if line !=0 or line > len(history)-1 :  
  148.             line = len(history)-1  
  149.         else:  
  150.             line = line+1  
  151.         char = history[line]  
  152.         cliwin.clear()  
  153.         cliwin.addstr(1,1,char)  
  154.         #print("down",line)  
  155.         #print(history[line])  
  156.     if key == curses.KEY_HOME:  
  157.         #subwin = screen.subwin(0, width, 00)  
  158.         screen.addstr(1,1,'\n'.join(history))  
  159.   
  160.     if key == curses.KEY_END:  
  161.         print(char)  
  162.       
  163.     if key == 27:  
  164.         menu(screen)  
  165.     #KEY_BACKSPACE  
  166.     #KEY_NPAGE KEY_PPAGE  
  167.     # if x == ord('1'):  
  168.     #      username = get_param("Enter the username")  
  169.     #      homedir = get_param("Enter the home directory, eg /home/nate")  
  170.     #      groups = get_param("Enter comma-separated groups, eg adm,dialout,cdrom")  
  171.     #      shell = get_param("Enter the shell, eg /bin/bash:")  
  172.     #      curses.endwin()  
  173.     #      execute_cmd("useradd -d " + homedir + " -g 1000 -G " + groups + " -m -s " + shell + " " + username)  
  174.     # if x == ord('2'):  
  175.     #      curses.endwin()  
  176.     #      execute_cmd("apachectl restart")  
  177.     # if x == ord('3'):  
  178.     #      curses.endwin()  
  179.     #      execute_cmd("df -h")  
  180.     #  
  181.     #exit()  
  182.     #screen.refresh()  
  183. screen.keypad(0)  
  184. curses.echo() ; curses.nocbreak()  
  185. screen.clear()  
  186. curses.endwin()  
  187.   
  188.   
  189.       
 
目录
相关文章
|
4天前
|
项目管理 开发工具 git
Python面试题:Git版本控制与协作开发
【4月更文挑战第19天】本文聚焦于Python面试中Git版本控制与协作开发的考察点,涵盖Git基础、协作流程及实战示例。面试者需理解仓库、提交、分支等核心概念,掌握常用命令,熟悉主干开发和GitFlow策略。在协作开发中,要掌握Pull Request工作流,有效处理合并冲突,并善用标签与里程碑。注意避免混淆工作区、忽视代码审查和直接在远程分支上工作等常见错误。通过实例展示了如何在GitFlow策略下合并分支和解决冲突,强调持续学习与实践以提升Git技能。
26 1
|
4天前
|
设计模式 开发框架 数据库
Python Web开发主要常用的框架
【5月更文挑战第12天】Python Web开发框架包括Django、Flask、Tornado和Pyramid。Django适用于复杂应用,提供ORM、模板引擎等全套功能;Flask轻量级,易于扩展,适合小型至中型项目;Tornado擅长处理高并发,支持异步和WebSockets;Pyramid灵活强大,可适配多种数据库和模板引擎,适用于各种规模项目。选择框架需依据项目需求和技术栈。
121 2
|
1天前
|
程序员 Python
python学习1:安装注意事项(1),2024年最新3个月学会Python开发
python学习1:安装注意事项(1),2024年最新3个月学会Python开发
python学习1:安装注意事项(1),2024年最新3个月学会Python开发
|
4天前
|
JSON 缓存 Java
|
4天前
|
Python
LabVIEW和Python开发微细车削控制系统
LabVIEW和Python开发微细车削控制系统
LabVIEW和Python开发微细车削控制系统
|
4天前
|
安全 测试技术 持续交付
在Python Web开发中,测试是一个至关重要的环节
【5月更文挑战第12天】在Python Web开发中,测试至关重要,包括单元测试(unittest模块)、集成测试、功能测试、系统测试、验收测试、性能测试、安全测试和端到端测试。常用的测试工具有unittest、pytest、selenium、requests和coverage。遵循“测试先行”和“持续集成”原则,确保代码质量与稳定性。
141 3
|
4天前
|
存储 程序员 API
python web开发示例详解
python web开发示例详解
18 0
|
4天前
|
Shell 测试技术 Python
在Mac上用Python调用终端执行命令
在Mac上用Python调用终端执行命令
15 1
|
4天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
43 0
|
4天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
34 0