0

我的问题的症结是我不想定义字典,然后当我应该能够使用它的名称调用整个字典时,手动将该字典的一部分交给一个函数。


我正在使用prompt_toolkit终端中的彩色打印。下面介绍了一个非常基本的可重现示例。它类似于prompt_toolkit 支持文档中的示例。

"""Stack Reproducible Example."""

from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.styles import Style

my_palette = { # A dictionary with colours I want to use
    "MY_PINK": '#ff1493',
    "MY_BLUE": '#0000ff',
}

text = FormattedText([ # defines the text...
    ('class:pink', 'Hello'), # ...Hello...
    ('', ' '),
    ('class:blue', 'World'), # ...World
])

style = Style.from_dict({ # The style sheet, feat. my own colours
    'pink': my_palette["MY_PINK"], # my pink colour
    'blue': (my_palette["MY_BLUE"] + " italic"), # my blue plus a type style
})

print_formatted_text(text, style=style) # prints stylised `Hello World`.

但是,您会看到我正在定义my_palette,然后手动将每一行赋予Style. 我只想一口气交出来。

显然我不明白到底发生了什么,Style尽管它很简单..

我尝试过的一些解决方案的实例:

style = Style.from_dict(my_palette)
style = Style(my_palette)
style = Style([my_palette])
style = Style.from_dict(repr(my_palette))

print_formatted_text(text, style=my_palette)
print_formatted_text(text, style=str(my_palette))

我怎样才能去掉“中间人”,直接把我自己的命令交给风格?

4

0 回答 0