• 技术文章 >常见问题 >Python常见问题

    如何使用Python编写文本菜单

    月亮邮递员月亮邮递员2020-03-30 09:55:01原创6330

    如何使用Python编写文本菜单

    什么是文本菜单?

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

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

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

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

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

    menu.py代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

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

    效果如下:

    002zniKvzy78cOQS64D87&690.jpg

    002zniKvzy78cORJbP836&690.jpg

    更多技术请关注Python视频教程

    专题推荐:python 文本菜单
    上一篇:Python如何实现excel筛选后生成新表 下一篇:如何将notepad绑定Python文件

    相关文章推荐

    • Python静态函数和普通方法的区别• Python脚本如何使用• Python的md5是什么意思• Python如何实现excel筛选后生成新表

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网