Skip to content Skip to sidebar Skip to footer

Display The User Input Text In Color In The Windows Cmd

I have yet another question regarding my little console program for the windows cmd. I use colorama to color my text in the terminal which makes it look like this: Then I found ou

Solution 1:

Removing the init(autoreset=True) line from your code runs as you wish on my machine.

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

My colorama version colorama==0.3.9.

The Colorama docs state that when using autoreset=true it will reset your colour and styling options immediately after the print command, this happens before you get to your input command which is why you do not get the colours in the user typed text.

Solution 2:

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

You can pass input() a string to display before the actual input of the user.

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)

You can probably get rid of the print(..., end='') call with this.

Post a Comment for "Display The User Input Text In Color In The Windows Cmd"