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.       
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值