温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python编写文本菜单的方法

发布时间:2020-07-17 10:28:10 来源:亿速云 阅读:1355 作者:清晨 栏目:编程语言

小编给大家分享一下使用Python编写文本菜单的方法,相信大部分人都还不怎么了解,因此分享这边文章给大家学习,希望大家阅读完这篇文章后大所收获,下面让我们一起去学习方法吧!

如何使用Python编写文本菜单

什么是文本菜单?

简单一句话,现在你能看到的都是图形菜单界面,退后20年,你能看到都是文本菜单界面。

文本菜单界面通常在以前比较老的DOS软件里见到,例如老的PCTOOLS软件,现在已经不容易找到了。

目前在windows系统下的软件界面一般都是图形菜单界面。

如何来实现文本菜单式的交互呢?

menu.py,运行python menu.py即可。

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()

效果如下:

使用Python编写文本菜单的方法

使用Python编写文本菜单的方法

以上是使用Python编写文本菜单的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI