Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
colored input()
#1
Hello,

How would I colored input prompt? I know how to when printing but not input.

I got this so far.

from colorama import init
from termcolor import colored

init()

def computer_prompt():
    return (colored('Computer: ', 'green', 'on_blue'))
def user_prompt():
    return (colored('User: ', 'yellow', 'on_blue'))

print(computer_prompt()+'Please enter a word.')

word = input(user_prompt()+('?: '))

print(word)
You can see that 'Computer: ' returns with green text but how do I color the 'User: ' input?

I get the following error when running this code:

Error:
<-[44m<-[33m?: <-[0m?:
I thought it was as easy as creating a function like computer_prompt but I just couldn't get the result I was looking for.

Please help.

Thank You.
Reply
#2
İmage


You can write a wrapper function for all input and output colors.
Something like this

from colorama import init
from colorama import Fore as F
from colorama import Back as B
from colorama import Style

def color(fore='', back='', text):
    return f'{fore}{back}{text}{Style.RESET_ALL}'

print(color(F.GREE, B.BLUE, 'Hello there!'))
You may also want to look at prompt_toolkit It's a powerful library which can do a lot more instead of just colored terminal.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hello Wavic,

When I ran this code..
from colorama import init, Fore, Back, Style
init()
word = input(Fore.YELLOW + Back.BLUE + '?: ' + Style.RESET_ALL)
I get the follow error
Error:
<-[33m<-[44m?: <-[0m
I am not sure what it means.

Please help.
Reply
#4
Post the full error traceback please.

I think it's something with your terminal.



Also, I see that there is an error in my function definition example. The arguments are in a wrong order.

Read this: https://en.wikipedia.org/wiki/ANSI_escape_code
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
input() might be escaping the color codes. Try just printing it out:
print(Fore.YELLOW + Back.BLUE + '?: ' + Style.RESET_ALL, end="")
word = input()
Reply
#6
Nilamno,

That works perfectly.

Thank you Nilamo and Wavic for your help.

Trianne
Reply
#7
Hm! Why is that behavior of the input? May be you can see that I get the expected.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I classify colored images into 3 classes max22 0 671 Dec-04-2023, 10:33 PM
Last Post: max22
Question Colored text Alivegamer 3 2,099 Feb-28-2022, 02:43 AM
Last Post: BashBedlam
  fontforge Emoji encoding and colored glyphs pauf28 0 2,143 Dec-22-2020, 10:05 AM
Last Post: pauf28
  Error printing colored text julio2000 0 1,485 Feb-02-2020, 07:04 PM
Last Post: julio2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020