[Tutor] Changing the text color in the WinXP dos shell (WinXP/py2.6.2)

Katt the_only_katala at verizon.net
Fri Oct 16 09:36:24 CEST 2009


> Date: Thu, 15 Oct 2009 08:11:11 +0100
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Changing the color of text in the windows shell
> (WinXP/python 2.6.2)
> Message-ID: <hb6huh$cba$1 at ger.gmane.org>
>
> "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.

Got that.  I remeber in the old world of BBS's creating ANSI screens for the 
WWIV software.  This shouldn't be any different just in a different 
language.

>
>> --------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)
>

Thank you for the code snippet.  At first when I tried this code it was 
printing correctly and following up with an error.  It was just because the 
end of the statement was missing the extra ]) at the end.  Then after that I 
was wondering why the phrase "There are" was invisible.  Realized that was 
because the color number was 0.

That was very helpful.

Thanks again,

Katt 



More information about the Tutor mailing list