简体   繁体   中英

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?

PS: It's pretty common knowledge that XOXO means "hugs and kisses", so it may not be appropriate in some working environments.

You have to define Your new theme and pass it as a parameter to inquirer.prompt .

Here is modified code changing "X" to "Y" and "o" to "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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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