Python Forum
ANSI not working for change of text colors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ANSI not working for change of text colors
#1
Hello,

I tried to change the text color in my python program, and looked up a tutorial. When I typed in what they wrote, it just printed: '[2;31;43m Text Color Test'. Is there any way to fix this? If not, are there other ways to change text colors?
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#2
Please show your code, hard to say without seeing what to tried to do.
Reply
#3
'\033[2;31;43m Text Color Test'

But you should use a library instead of embedding ascii codes.

https://www.geeksforgeeks.org/print-colo...-terminal/
BliepMonster likes this post
Reply
#4
(Nov-03-2022, 03:53 PM)deanhystad Wrote: '\033[2;31;43m Text Color Test'

But you should use a library instead of embedding ascii codes.

https://www.geeksforgeeks.org/print-colo...-terminal/
I tried to use the colorama, but it just said the same as the ANSII.
Output:
some red text and with a green background and in dim text  back to normal now
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#5
What shell are you using? What os? What Python?
Reply
#6
(Nov-03-2022, 04:15 PM)BliepMonster Wrote: I tried to use the colorama, but it just said the same as the ANSII.
It want work because you use cmd,a better shell like cmder would work.

Shoul use Rich ✨ it's in a league of own when it come to color in Terminal for Python.
Rich also work in cmd.
from rich import print

print("[red]Python Forum[/red]")
print("[bold blue]Hello world[/bold blue]!")

d = {
    "bool1": True,
    "bool2": False,
    "number": 123.45,
    "string": "Hello!",
    "dict": {"key": "value"},
    "list": [1, 2, 3],
    "class": type(1),
}

print(d)
cmd:
[Image: EEOdHI.png]

cmder:
[Image: zHdpjo.png]
Reply
#7
(Nov-03-2022, 05:19 PM)deanhystad Wrote: What shell are you using? What os? What Python?

I am using python 3.9 and the Windows PowerShell to install libraries. I don't know what OS.
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#8
(Nov-03-2022, 05:22 PM)snippsat Wrote:
(Nov-03-2022, 04:15 PM)BliepMonster Wrote: I tried to use the colorama, but it just said the same as the ANSII.
It want work because you use cmd,a better shell like cmder would work.

Shoul use Rich ✨ it's in a league of own when it come to color in Terminal for Python.
Rich also work in cmd.
from rich import print

print("[red]Python Forum[/red]")
print("[bold blue]Hello world[/bold blue]!")

d = {
    "bool1": True,
    "bool2": False,
    "number": 123.45,
    "string": "Hello!",
    "dict": {"key": "value"},
    "list": [1, 2, 3],
    "class": type(1),
}

print(d)
cmd:
[Image: EEOdHI.png]

cmder:
[Image: zHdpjo.png]

I installed rich, and copy pasted your command, and it doesnt show the colors.
Output:
Python Forum Hello world! { 'bool1': True, 'bool2': False, 'number': 123.45, 'string': 'Hello!', 'dict': {'key': 'value'}, 'list': [1, 2, 3], 'class': <class 'int'> }
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#9
This explains the problem you are having:

https://duffney.io/usingansiescapesequencespowershell/
Reply
#10
BliepMonster Wrote:I am using python 3.9 and the Windows PowerShell to install libraries. I don't know what OS.
OS means Operating System and you should know that you use Windows👀
Are running code in PowerShell?
You can not use a Editor as a color will only show using commanl line.
(Nov-04-2022, 11:18 AM)deanhystad Wrote: This explains the problem you are having:

https://duffney.io/usingansiescapesequencespowershell/
No this don't apply to Rich,it's has been testet on all OS and diffrent shell.
So Rich has overcome all ANSI problems within PowerShell and cmd.
If i do test that as i have not with PowerShell(as i use cmder),pretty sure that it will be working using Rich.
So if i do a test in PowerShell,colors🎨 work fine.
[Image: JQ6ojE.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count image's colors very fast flash77 18 1,588 Mar-05-2024, 06:12 PM
Last Post: deanhystad
  Text conversion to lowercase is not working ineuw 3 479 Jan-16-2024, 02:42 AM
Last Post: ineuw
  Decryption not working if key has same symbol like text Paragoon2 0 320 Nov-11-2023, 09:32 PM
Last Post: Paragoon2
  can openpyxl read font colors mperemsky 3 1,754 May-09-2023, 11:18 AM
Last Post: MindKeeper
  while loop not working-I am using sublime text editor mma_python 4 1,149 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  find some word in text list file and a bit change to them RolanRoll 3 1,549 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to do bar graph with positive and negative values different colors? Mark17 1 5,171 Jun-10-2022, 07:38 PM
Last Post: Mark17
  Change Text Size in Console? ShakeyPakey 9 11,242 Dec-09-2021, 02:51 AM
Last Post: drvlwho
  [split] Change Text Size in Console? CodingKid 1 1,889 Jul-20-2021, 02:47 PM
Last Post: deanhystad
  Plot Back Ground Colors JoeDainton123 0 2,197 Aug-19-2020, 11:09 PM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

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