如何将 Python 询问者复选框的 select / unselect 符号从 X 和 o 分别更改为 Y 和 N?
How to change Python inquirer checkbox's select / unselect symbol from X and o to Y and N respectively?
示例脚本:
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”?
PS: 众所周知,XOXO 的意思是“拥抱和亲吻”,因此在某些工作环境中可能不合适。
您必须定义您的新主题并将其作为参数传递给 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))
示例脚本:
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”?
PS: 众所周知,XOXO 的意思是“拥抱和亲吻”,因此在某些工作环境中可能不合适。
您必须定义您的新主题并将其作为参数传递给 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))