如何为控制台、终端应用程序创建菜单系统

How to create a menu system for a console, terminal application

我寻找“如何为控制台、终端应用程序创建菜单系统”。 None 提出的类似问题完全回答了我的目标。

  1. 向控制台应用程序添加菜单以管理活动。
  2. 运行 一个选定的函数。
  3. 清除输出
  4. 如果选择完成,则再次显示菜单或退出

因此,我将其作为一种问答形式来处理。 It’s OK to Ask and Answer Your Own Questions

欢迎提出更好的答案。

"""
1. Add a menu to a console application to manage activities.
2. Run a selected function.
3. Clear the output
4. Display the menu again or exit if done is selected
"""

import sys
from os import system


def display_menu(menu):
    """
    Display a menu where the key identifies the name of a function.
    :param menu: dictionary, key identifies a value which is a function name
    :return:
    """
    for k, function in menu.items():
        print(k, function.__name__)


def one():
    print("you have selected menu option one") # Simulate function output.
    input("Press Enter to Continue\n")
    system('cls')  # clears stdout


def two():
    print("you have selected menu option two") # Simulate function output.
    input("Press Enter to Continue\n")
    system('cls')  # clears stdout


def three():
    print("you have selected menu option three") # Simulate function output.
    input("Press Enter to Continue\n")
    system('cls')  # clears stdout


def done():
    system('cls')  # clears stdout
    print("Goodbye")
    sys.exit()


def main():
    # Create a menu dictionary where the key is an integer number and the
    # value is a function name.
    functions_names = [one, two, three, done]
    menu_items = dict(enumerate(functions_names, start=1))

    while True:
        display_menu(menu_items)
        selection = int(
            input("Please enter your selection number: "))  # Get function key
        selected_value = menu_items[selection]  # Gets the function name
        selected_value()  # add parentheses to call the function


if __name__ == "__main__":
    main()