python_pardus
python_pardus

Reputation: 320

How to set background color of console in python?

My question is clear. How can I set background color of console with python ? I know that we can set background color of console in C#. So I thought that python can do it, too. But how ? I'm on Windows and I use python 2.7.3. Thanks...

Upvotes: 5

Views: 17754

Answers (2)

yask
yask

Reputation: 4278

Depends on the shell and terminal emulator you are working running on. For bash this should work on ubuntu's terminal.

import os
color = 'blue'
os.system('setterm -term linux -back $'+color+' -fore white -clear')

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49318

You can set the Windows cmd console color with the color command, passing it from Python via os.system:

>>> import os
>>> os.system('color 1f') # sets the background to blue

For Linux terminals, use the setterm command:

>>> import os
>>> os.system('setterm -background white -foreground white -store')

Upvotes: 8

Related Questions