• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何使用Python编写文本菜单

python 搞java代码 3年前 (2022-05-21) 22次浏览 已收录 0个评论

如何使用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()
www#gaodaima.com来源gaodai.ma#com搞##代!^码网搞代码

效果如下:

更多技术请关注云海天Python教程。

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何使用Python编写文本菜单

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址