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

如何使用python编写文本菜单(老式菜单)

(2017-01-23 10:40:01)
标签:

it

杂谈

分类: 工作
       什么是文本菜单?
简单一句话,现在你能看到的都是图形菜单界面,退后20年,你能看到都是文本菜单界面。

       文本菜单界面通常在以前比较老的DOS软件里见到,例如老的PCTOOLS软件,现在已经不容易找到了。
目前在windows系统下的软件界面一般都是图形菜单界面。

       如何来实现文本菜单式的交互呢?
将menu.py,运行python menu.py即可。

------menu.py----------
#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Edit: turnipsmart.com

import os,sys

running = True
menu = """
  Main Menu  
--------------------
 1: Display Options
 2: Config  Options
 3: Deteting
 h: Help
 q: Quit
--------------------
"""

menu_dict={
      "h": "Please enter the options to be operated.",
      "1": "df -h",
      "2": "free -m",
      "3": "netstat -lnt",
      }
 
def commands(args):
    cmd = menu_dict.get(args)
    return cmd
 
if __name__ == "__main__":
    os.system('cls')
    print menu   
    while running:
        cmd = raw_input("Input Your Commond:")
        if cmd != 'q':
            os.system('cls')
            try:
                print menu
                if commands(cmd) != None:
                    #fo = os.popen(commands(cmd))
                    #print fo.read()
                    if cmd == '1':
                        print "cmd=1"

                    elif  cmd == '2':
                        print "cmd=2"

                    elif  cmd == '3':
                        print "cmd=3"

                    else:
                        print commands(cmd)
                else:
                    print "Input is Wrong!"
            except Exception,e:
                print menu
                print e            
        else:
            print 'We will exit the menu.'
            os.system('cls')
            sys.exit()
---------------------
http://www.th7.cn/Program/Python/201507/498646.shtml

0

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

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

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

新浪公司 版权所有