更改Python中prompt_toolkit提示符的占位符文本的颜色?

htrmnn0y  于 2023-03-07  发布在  Python
关注(0)|答案(1)|浏览(172)

下面是导致此错误的代码:TypeError: argument of type 'HTML' is not iterable

from prompt_toolkit import prompt
from prompt_toolkit.formatted_text import FormattedText, HTML

def get_input():
    prompt_text = [
        ("class:prompt-prefix", HTML('<ansired><b>{}</b></ansired>'.format('your input > '))),
        ("", '')
    ]
    placeholder = HTML('<b>enter `q` or `exit` to exit </b>')
    return prompt(prompt_text, placeholder=placeholder, default='').lower()

我进行了测试,发现是prompt_text引入了错误,我将其更改为:

prompt_text = HTML('<ansired><b>{}</b></ansired>'.format('your input \> '))
# escape > character or not does not matter, I found

还是不行,怎么修复这个bug?

2ul0zpep

2ul0zpep1#

经过一些尝试和错误,我做到了:

from prompt_toolkit import prompt
from prompt_toolkit.formatted_text import FormattedText, HTML
from prompt_toolkit.styles import Style

def get_input():
    prompt_style = Style.from_dict({
        'prompt': 'ansigreen bold',
        'input': '',
    })
    prompt_text = 'your input > '

    placeholder = HTML('<ansigray> enter `q` or `exit` to exit</ansigray>')
    return prompt(prompt_text, placeholder=placeholder, default='', style=prompt_style).lower()

相关问题