[Tutor] Changing the color of text in the windows shell (WinXP/python 2.6.2)

Alan Gauld alan.gauld at btinternet.com
Thu Oct 15 09:11:11 CEST 2009


"Katt" <the_only_katala at verizon.net> wrote

> lucky).  So I will pursue all three types of changing color.
>
> First I will try WConio, next to be followed by EasyGUI,

EasyGUI won;t help change colour, it is just a way of making
a command line script act like a GUI - by popping up dialog
boxes to capture input and dsplay messages. It replaces
raw_input and print with dialogs.

> --------code--------
> from WConio import textcolor
>
> apples_left = 5
>
> print "There are",(textcolor(4)),apples_left,(textcolor(7)),"left in the 
> basket."
> ---------------------
>
> The output works, but there is one snag.  I think that I am leaving 
> something out because it has the word "None" on both sides of the 
> variable.

The textcolor() function returns None. so you need to keep it
out of your print statement:. This means you need to split your
print into multiple separate statements. (This will also be true
for the pywin32 version)

print "There are",
textcolor(4)
print apples_left,
textcolor(7)
print "left in the basket."

The way I'd handle thus is to create a function which takes a
list of tuples as input, with each tuple containing the string
and its colour:

def colorPrint(strings):
    for string in strings:
         textcolor(string[1])
         print string[0],

colorPrint([("There are", 0),(str(apples_left),4),("left in the 
basket.",7),("\n",0)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list