简体   繁体   English

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

[英]How to change Python inquirer checkbox's select / unselect symbol from X and o to Y and N respectively?

The sample script:示例脚本:

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

produces:产生:

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

How do I change "X" and "o" to "Y" and "N" respectively?如何将“X”和“o”分别更改为“Y”和“N”?

PS: It's pretty common knowledge that XOXO means "hugs and kisses", so it may not be appropriate in some working environments. PS:众所周知,XOXO 的意思是“拥抱和亲吻”,因此在某些工作环境中可能不合适。

You have to define Your new theme and pass it as a parameter to inquirer.prompt .您必须定义您的新主题并将其作为参数传递给inquirer.prompt

Here is modified code changing "X" to "Y" and "o" to "N":这是修改后的代码,将“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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM