5

I found useful package rich. And want to use it in the next way: drawing in different colors words in sentence and at different background colors. I use next code:

from rich.console import Console
console = Console()
console.print('[green]some[/green] [#F47983]text[/#F47983]')

This way I can set any color for any word:

enter image description here

But I don't know, how to set different background colors. I tried background= , bg: and backgrounp-color - this doesn't work :(

Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102

1 Answers1

7

Use the word "on" to introduce the background color. For example:

print("[bold green on blue]Fear is the mind-killer[/]")

Also if you want to use hex codes:

print('[green on #F47983]some[/] [#F47983]text[/]')

See https://rich.readthedocs.io/en/latest/style.html for how to define styles.

oberbaum
  • 2,451
  • 7
  • 36
  • 52
Will McGugan
  • 2,005
  • 13
  • 10
  • It works! Thank you! Furthermore, it works next way too: `console.print('[green on #F47983]some[/green on #F47983] [#F47983]text[/#F47983]') ` I added info to your answer :) – Mikhail_Sam Jul 17 '20 at 09:03
  • but how would you change the color of whole console in python.this only changes the background colour of the text we print. – Justaus3r Mar 18 '21 at 12:14
  • Only way would be to set your terminal theme. The color codes will only have an effect on text that is subsequently written. – Will McGugan Mar 19 '21 at 13:25