(Sep-17-2019, 03:14 AM)wavic Wrote: [ -> ]https://pypi.org/project/colorama/ ?
https://python-prompt-toolkit.readthedoc..._text.html ?
I'm not sure how to deal with the text size
Thank you very much indeed!
I had seen about
colorama, searching for colouring strings, but I didn't know about
termcolor.
I was a bit worried about these kinds of packages, because I'm a newbie, and you need to import them and make other modifications inside your programs to make them work... and I was right... I don't know what I'm doing wrong, but I cannot make it work in my Windows 10. I'm using Python 3.7.4 and somewhere I read that it's with batteries included... Well, I beg to differ... I need colour in my world, and I think it should be included within the original Python download package, but oh, blimey, I cannot do something about it.
I'm going to show what I have done, and maybe somebody is kind enough to indicate me what I'm doing wrong.
Okay, first, I gave colorama a try, so I used pip installation, which seems to have been successful:
![[Image: successful-colorama-installation.png]](https://i.postimg.cc/1tJKZKqf/successful-colorama-installation.png)
Then I tried a test program:
import colorama
from colorama import init
init()
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
print('\033[31m' + 'some red text')
print('\033[30m') # and reset to default color
with the following failed output:
Output:
<-[31msome red text
<-[42mand with a green background
<-[2mand in dim text
<-[0m
back to normal now
<-[31msome red text
<-[30m
Then I tried with a different test program from a different site:
import colorama
# If using Windows, init() will cause anything sent to stdout or stderr
# will have ANSI color codes converted to the Windows versions. Hooray!
# If you are already using an ANSI compliant shell, it won't do anything
colorama.init()
# Now regular ANSI codes should work, even in Windows
CLEAR_SCREEN = '\033[2J'
RED = '\033[31m' # mode 31 = red forground
RESET = '\033[0m' # mode 0 = reset
print(CLEAR_SCREEN + RED + 'Welcome!' + RESET)
producing an equally failed output:
Output:
<-[2J<-[31mWelcome!<-[0m
I tried with a different test program from another site:
import colorama
from colorama import Fore, Back, Style
colorama.init()
# Set the color semi-permanently
print(Fore.CYAN)
print("Text will continue to be cyan")
print("until it is reset or changed")
print(Style.RESET_ALL)
# Colorize a single line and then reset
print(Fore.RED + 'You can colorize a single line.' + Style.RESET_ALL)
# Colorize a single word in the output
print('Or a single ' + Back.GREEN + 'words' + Style.RESET_ALL + ' can be highlighted')
# Combine foreground and background color
print(Fore.BLUE + Back.WHITE)
print('Foreground, background, and styles can be combined')
print("========== ")
print(Style.RESET_ALL)
print('If unsure, reset everything back to normal.')
and another fail:
Output:
<-[36m
Text will continue to be cyan
until it is reset or changed
<-[0m
<-[31mYou can colorize a single line.<-[0m
Or a single <-[42mwords<-[0m can be highlighted
<-[34m<-[47m
Foreground, background, and styles can be combined
==========
<-[0m
If unsure, reset everything back to normal.
So I decided then to give termcolor a try, and also it seems that it was a successful installation:
![[Image: successful-termcolor-installation.png]](https://i.postimg.cc/CM9v7Sbt/successful-termcolor-installation.png)
Then I used the following test program from the termcolor site:
import sys
from termcolor import colored, cprint
text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)
cprint('Hello, World!', 'green', 'on_red')
print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan')
print_red_on_cyan('Hello, World!')
print_red_on_cyan('Hello, Universe!')
for i in range(10):
cprint(i, 'magenta', end=' ')
cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
with the following once more failed attempt:
Output:
<-[5m<-[7m<-[31mHello, World!<-[0m
<-[41m<-[32mHello, World!<-[0m
<-[46m<-[31mHello, World!<-[0m
<-[46m<-[31mHello, Universe!<-[0m
<-[35m0<-[0m <-[35m1<-[0m <-[35m2<-[0m <-[35m3<-[0m <-[35m4<-[0m <-[35m5<-[0m <-[35m6<-[0m <-[35m7<-[0m <-[35m8<-[0m <-[35m9<-[0m <-[1m<-[31mAttention!<-[0m
Thanks in advance, any help would be appreciated.
All the best,
I just noticed that indeed the last part of the output shown for termcolor was indeed printed in red, but just the last part. And as it says Attention!, probably my mind played a trick on me. It was shown in red, not in bold type as it should, but at least it could help to find an answer. This was printed in red (like an error message) at the end of the output (the rest of the output was shown in the regular blue coloured font type on a white background colour, in my Python 3.7.4 Shell running on Windows 10:
<-[1m<-[31mAttention!<-[0m