代码之家  ›  专栏  ›  技术社区  ›  Cut7er

在windows命令中以彩色显示用户输入的文本

  •  0
  • Cut7er  · 技术社区  · 6 年前

    我还有一个关于我的windows cmd小控制台程序的问题。

    我使用colorama在终端中为文本上色,使其看起来像这样:

    enter image description here

    然后我发现了如何在一个 input()

    from colorama import init
    init(autoreset=True)
    YELLOW = "\x1b[1;33;40m" 
    
    print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
    tournament = input()
    

    这就引出了上图中的黄线。

    但是我仍然在寻找一种方法来给用户输入的字符上色——所以在这里我想把用户输入的“sdffdgf…”也格式化成彩色。

    enter image description here

    有人给我一个解决方案,或者它只是不可能与有限的windows命令?

    2 回复  |  直到 6 年前
        1
  •  1
  •   WhatsThePoint    6 年前

    卸下 init(autoreset=True) 代码中的行在我的机器上按您的意愿运行。

    import colorama
    
    from colorama import Fore,Style,Back
    colorama.init()
    
    YELLOW = "\x1b[1;33;40m" 
    RED = "\x1b[1;31;40m"
    
    print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
    tournament = input()
    print(f"\n{RED}Turnier spielen? [T]: ", end='')
    tournament2 = input()
    

    code together with output to see it working

    colorama==0.3.9 .

    这个 Colorama docs 说明使用时 autoreset=true print 命令,这发生在你进入你的输入命令之前,这就是为什么你不能在用户输入的文本中得到颜色。

        2
  •  1
  •   Ph3n0x    6 年前

    https://docs.python.org/3/library/functions.html#input

    你可以通过 input()

    from colorama import init
    init(autoreset=True)
    YELLOW = "\x1b[1;33;40m"
    RED = "\x1b[1;31;40m"
    
    print(f"\n{YELLOW}Turnier spielen? [T]: ", end='')
    tournament = input(RED)
    

    你也许可以摆脱 print(..., end='')

    推荐文章