知行编程网知行编程网  2022-09-21 03:30 知行编程网 隐藏边栏  204 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于如何使用Python编写文本菜单的相关知识,包括编程实现窗口菜单,以及编写一个菜单显示程序这些编程知识,希望对大家有参考作用。

如何在 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 中编写文本菜单

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享