from pypinyin import pinyin, Style# 定义菜单项和对应的子菜单 menu = {"文件": {"新建": {},"打开": {},"保存": {}},"编辑": {"撤销": {},"重做": {},"剪切": {},"复制": {},"粘贴": {}},"视图": {"查看": {"大图标": {},"小图标": {},"列表": {},"详细信息": {}}},"帮助": {"查看帮助": {},"关于": {}} }def print_menu(menu, level=0):for key, value in menu.items():# 打印菜单项名称print(" " * level + key)# 打印子菜单if value:print_menu(value, level + 1)def print_pinyin_menu(menu, level=0):for key, value in menu.items():# 将中文转换成拼音pinyin_name = ''.join(pinyin(key, style=Style.NORMAL, heteronym=False)[0])# 打印菜单项名称和拼音print(" " * level + key + " (" + pinyin_name + ")")# 打印子菜单if value:print_pinyin_menu(value, level + 1)print("========= 中文菜单 =========") print_menu(menu)print("\n========= 拼音菜单 =========") print_pinyin_menu(menu)