Member Avatar for leegeorg07

Hi, is there an internal module / method to change the colour of the text in the pythonwin?

I am asking this so that I can make more exciting text based games

Recommended Answers

All 4 Replies

If you have the Windows OS, here is an example ...

# change color in the python.exe console display (Windows)
# color is a two digit hexadecimal number
# the first digit is the background
# the second digit is the foreground (text)
# 0=black 1=blue 2=green ... to E=yellow F=white

import os

os.system("color F2")  # green(2) text on white(F) background
Member Avatar for leegeorg07

thanks, I used that and a bit of knowledge to make this:

import os, time
colours = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f')
def color(i):
    i = i.lower()
    if i == 'black':
        return 0
    if i == 'blue':
        return 1
    if i == 'green':
        return 2
    if i == 'aqua':
        return 3
    if i == 'red':
        return 4
    if i == 'purple':
        return 5
    if i == 'yellow':
        return 6
    if i == 'white':
        return 7
    if i == 'grey':
        return 8
    if i == 'light blue':
        return 9
    if i == 'light green':
        return 'a'
    if i == 'light aqua':
        return 'b'
    if i == 'light red':
        return 'c'
    if i == 'light purple':
        return 'd'
    if i == 'light yellow':
        return 'e'
    if i == 'bright white':
        return 'f'
text_colour = raw_input("""
What colour do you want the text?
Black
Blue
Green
Aqua
Red
Purple
Yellow
White
Grey
Light Blue
Light Green
Light Aqua
Light Red
Light Purple
Light Yellow
Bright White
""")

bgc = raw_input("""
What colour do you want the text?
Black
Blue
Green
Aqua
Red
Purple
Yellow
White
Grey
Light Blue
Light Green
Light Aqua
Light Red
Light Purple
Light Yellow
Bright White
""")
a = color(bgc)
b = color(text_colour)
c = 'color '+str(a)+str(b)
os.system(c)
print ''
print c
time.sleep(3)

I know that this is slightly unnecessary but I can't stand the sight of the above code. I don't know if your colour codes are right (I didn't test them), but this is just a much cleaner way of writing your little program. Just putting the simplicity of Python to good use, that's all.

# this works, but I just don't know if the colour codes are correct...
import os, time

colourcodes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
colournames = ['black', 'blue', 'green', 'aqua', 'red', 'purple', 'yellow', 'white', 'grey', 'light blue', 'light green', 'light aqua', 'light red', 'light purple', 'light yellow', 'bright white']

def color(i):
	i = i.lower()
	return colourcodes[colournames.index(i)]
	
print '\n'.join([c.title() for c in colournames])

txc = raw_input('\nWhat colour do you want the text?  ')
bgc = raw_input('\nWhat colour do you want the background?  ')
a = color(bgc)
b = color(txc)
c = 'color %s%s' % (a, b)

os.system(c)
print '\n' + c
time.sleep(3)
Member Avatar for leegeorg07

wow thanks, that worked even better, ill post my new import version in a minute

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.