首页 > 编程语言 >2023-02-09-使用simple_term_menu创建更好的python菜单

2023-02-09-使用simple_term_menu创建更好的python菜单

时间:2023-10-09 19:25:50浏览次数:44  
标签:02 index term 菜单 simple menu choices

+++
title = "用 simple_term_menu 创建更好的python菜单"
description = ""
date = 2023-02-09T16:25:24+08:00
featured = false
comment = true
toc = true
reward = true
categories = [
""
]
tags = [
"python","menu"
]
series = []
images = []
+++

安装

pip install simple_term_menu

⚠️ 不支持windows
这个菜单库使用非常简单,只需要传入一个列表,就可以生成一个菜单,返回值就是选择的菜单项的索引。

  • 支持J K/⬆️ ⬇️ 键移动
  • 支持/斜杠搜索
  • 支持快捷键(传入的 string 如 [q] 退出 ,q 就变成快捷键了)
  • 好看!
  • 还有更多功能,我没有仔细研究,参考>这里<

封装成函数,方便调用

from simple_term_menu import TerminalMenu
def menu(choices:list):
    terminal_menu = TerminalMenu(choices,title="扫描结果转换XML工具 v1.0 (230208)\n---",show_search_hint=True)
    index = terminal_menu.show()

    print("执行", choices[index])
    if choices[index] == "[h] 文档":
        import webbrowser
        webbrowser.open('http://example.com')  
        return 0
    if choices[index] == "[q] 退出":
        exit()
    return index + 1 # 返回结果,从1开始

main 函数调用

if __name__ == '__main__':
    while True:
        # 获取功能列表,显示菜单
        choices_str = open('menu.md', 'r', encoding='utf-8').read()
        choices =choices_str.splitlines()
        choice_index = menu(choices)

其实一开始选择用md是因为写序号的话可以自动排序,但是加了序号和快捷键之后,就不美观了,所以把序号删除了。

加载菜单 menu.md

长亭xray(主机+应用) html 1->1
长亭xray(主机+应用) html 多->1
绿盟(主机) <ip>.html 1->1
绿盟(主机) <ip>.html 多->1
安恒新版 (应用) html 1->1
Nessus csv(utf-8) 1->1
Nessus csv(gb18030) 1->1
[h] 文档
[q] 退出

效果图:

对了这个东西还有个坑的地方,在pycharm运行的时候需要设置一下才能正常使用(要开启模拟终端):

标签:02,index,term,菜单,simple,menu,choices
From: https://www.cnblogs.com/kasusa/p/17752920.html

相关文章

  • 2023-02-09-dbdiagram
    +++title="Dbdiagram.io,数据库设计的好选择"description=""date=2023-02-09T16:01:23+08:00featured=falsecomment=truetoc=truereward=truecategories=[""]tags=["database","free"]series=[]......
  • 2023.10.9——每日总结
    学习所花时间(包括上课):0h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.DIV+CSS;明日计划:学习......
  • [BJDCTF2020]ZJCTF,不过如此
    原理关于preg_replace\e的代码执行双引号和单引号的区别可变变量解题过程代码审计<?phperror_reporting(0);$text=$_GET["text"];$file=$_GET["file"];if(isset($text)&&(file_get_contents($text,'r')==="Ihaveadream")){echo......
  • LY1380 [ 20231009 NOIP 模拟赛 T1 ] AK 神
    题意给定长度为\(n\)的序列\(S\)。\(A\),\(B\)两人轮流取连续\(k\)个数,保证\(n\equiv1\pmodk\)。\(A\)使最终数字更小,\(B\)使最终数字更大。问取到数的和。Sol直接考虑每次选哪些数,怎么选显然是不好做的。不难发现\(n\equiv1\pmodk\)的条件。题面提示我们......
  • 2023-01-05vscode技巧
    csharp程序不能输入用vscode运行csharp默认使用的terminal是不支持输入的,输入没反应需要修改launch.json增加这两行我估计是关闭了什么东西,并且调用系统的terminal,具体就不懂了。"configurations":[{//Formoreinformationaboutthe'console'field,se......
  • 2023-01-06-Regex
    最近工作终于闲了下来,可以上班研究代码。遇到一些需要正则表达式的编程问题,无法解决所以决定个学一下。Regex101-ZH-CN(regexlearn.com)RegexLearn-练习场RegexLearn-cheatsheet练习题简单匹配单词heisinblack.black. 允许匹配任何字符,包括特殊字符和空格......
  • 2023-02-06grub
    +++title="grubsettingsfornoobies"description=""date=2023-02-06T14:21:50+08:00featured=falsecomment=truetoc=truereward=truecategories=[""]tags=["ubuntu"]series=[]images=[]+++......
  • 2023-02-06Fix dual system time problem copy
    +++title="Fixdualsystemtimeproblem"description=""date=2023-02-06T14:21:50+08:00featured=falsecomment=truetoc=truereward=truecategories=[""]tags=["ubuntu"]series=[]images=[]+......
  • 2023-1-0xpython_beautiful_soup
    +++title="python_beautiful_soup"description=""date=2023-03-20T15:50:22+08:00featured=falsecomment=truetoc=truereward=truecategories=[""]tags=[""]series=[]images=[]+++还没写,先留着空位......
  • 2023-01-31python-path
    +++title="使用标准的path处理方法(Python)"description=""date=2023-01-31T15:26:05+08:00featured=falsecomment=truetoc=truereward=truecategories=[""]tags=["python"]series=[]images=[]+++标准方......