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.       
 
目录
相关文章
|
17天前
|
算法 测试技术 开发者
性能优化与代码审查:提升Python开发效率
【4月更文挑战第9天】本文强调了Python开发中性能优化和代码审查的重要性。性能优化包括选择合适数据结构、使用生成器和避免全局变量,而代码审查涉及遵循编码规范、使用静态代码分析工具和编写单元测试。这些实践能提升代码效率和可维护性,促进团队协作。
|
2月前
|
缓存 Unix C语言
涨见识了,在终端执行 Python 代码的 6 种方式!
涨见识了,在终端执行 Python 代码的 6 种方式!
32 0
|
2月前
|
机器学习/深度学习 设计模式 Java
Python潮流周刊#10:Twitter 的强敌 Threads 是用 Python 开发的!
Python潮流周刊#10:Twitter 的强敌 Threads 是用 Python 开发的!
31 2
|
3天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
13 0
|
3天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
6 0
|
3天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
7 0
|
8天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
11天前
|
前端开发 数据挖掘 API
使用Python中的Flask框架进行Web应用开发
【4月更文挑战第15天】在Python的Web开发领域,Flask是一个备受欢迎的轻量级Web框架。它简洁、灵活且易于扩展,使得开发者能够快速地构建出高质量的Web应用。本文将深入探讨Flask框架的核心特性、使用方法以及在实际开发中的应用。
|
15天前
|
JavaScript 前端开发 关系型数据库
金融技术解决方案:用Python和Vue开发加密货币交易平台
【4月更文挑战第11天】本文介绍了如何使用Python和Vue.js构建加密货币交易平台。首先确保安装了Python、Node.js、数据库系统和Git。后端可选择Flask或Django框架,通过RESTful API处理交易。前端利用Vue.js、Vuex和Vue Router创建用户友好的界面,并用Axios与后端通信。这种架构促进团队协作,提升代码质量和平台功能。
|
16天前
|
JavaScript 前端开发 Docker
全栈开发实战:结合Python、Vue和Docker进行部署
【4月更文挑战第10天】本文介绍了如何使用Python、Vue.js和Docker进行全栈开发和部署。Python搭配Flask创建后端API,Vue.js构建前端界面,Docker负责应用的容器化部署。通过编写Dockerfile,将Python应用构建成Docker镜像并运行,前端部分使用Vue CLI创建项目并与后端交互。最后,通过Nginx和另一个Dockerfile部署前端应用。这种组合提升了开发效率,保证了应用的可维护性和扩展性,适合不同规模的企业使用。