代码之家  ›  专栏  ›  技术社区  ›  puravidaso

如何将Python查询器复选框的选择/取消选择符号分别从X和o更改为Y和N?

  •  0
  • puravidaso  · 技术社区  · 4 年前

    示例脚本:

    import os
    import sys
    from pprint import pprint
    import yaml
    
    sys.path.append(os.path.realpath("."))
    import inquirer  # noqa
    
    questions = [
        inquirer.Checkbox(
            "interests",
            message="What are you interested in?",
            choices=["Computers", "Books", "Science", "Nature", "Fantasy", "History"],
            default=["Computers", "Books"],
        ),
    ]
    
    answers = inquirer.prompt(questions)
    
    pprint(answers)
    print(yaml.dump(answers))
    

    产生:

    [?] What are you interested in?: 
       X Computers
       o Books
    

    如何将“X”和“o”分别更改为“Y”和“N”?

    附言:众所周知,XOXO的意思是“拥抱和亲吻”,所以在某些工作环境中可能不合适。

    0 回复  |  直到 4 年前
        1
  •  1
  •   Domarm    4 年前

    你必须定义你的新主题,并将其作为参数传递给 inquirer.prompt

    以下是修改后的代码,将“X”更改为“Y”,将“o”更改为为“N”:

    import os
    import sys
    from pprint import pprint
    
    import yaml
    from inquirer.themes import Default
    
    sys.path.append(os.path.realpath("."))
    import inquirer  # noqa
    
    questions = [
        inquirer.Checkbox(
            "interests",
            message="What are you interested in?",
            choices=["Computers", "Books", "Science", "Nature", "Fantasy", "History"],
            default=["Computers", "Books"],
        ),
    ]
    
    
    class WorkplaceFriendlyTheme(Default):
        """Custom theme replacing X with Y and o with N"""
    
        def __init__(self):
            super().__init__()
            self.Checkbox.selected_icon = "Y"
            self.Checkbox.unselected_icon = "N"
    
    
    answers = inquirer.prompt(questions, theme=WorkplaceFriendlyTheme())
    
    pprint(answers)
    print(yaml.dump(answers))