Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Different Colors In Console Output (http://www.programmingforums.org/showthread.php?t=12556)

Sane Feb 11th, 2007 1:19 PM

Different Colors In Console Output
 
Quote:

Originally Posted by vBulletin Notice
Hello Sane it appears that you have not posted on our forums in several weeks, why not take a few moments to ask a question, help provide a solution or just engage in a conversation with another member in any one of our forums?

lol...

I am running Python 2.4 on Windows. How can I display console output in a different color?

All the solutions I find are Unix only.

I know how to do it in C, but not Python. So I know it must be possible. If it isn't, I'll just use wxWidgets.

Thanks for any help.

MicDareall Feb 11th, 2007 8:56 PM

Have you downloaded the win32 api extensions for python? Im not positive this is it because I haven't downloaded it in a while but after a quick google I think this is it.

https://sourceforge.net/project/show...group_id=78018

Dietrich Feb 12th, 2007 2:51 PM

Nice to see you back:
[PHP]# change color in the 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[/PHP]BTW, don't take DaWei serious, he is crude to most of us!

Sane Feb 12th, 2007 6:09 PM

@MicDareall:
Yes, I have.
@Dietrich:
Oh cool! The 16 color system! Brings back memories of Q-BASIC. :D

Thanks a bunch!!

I'm confused. Did DaWei post, then delete it? Also, I know of DaWei's habits. I've been a member here as long as you. :p

EDIT : Oh... there's a problem. This changes the font and background color everywhere. I needed this so that I could identify importance of certain labels in the output. In other words, I need to be able to change the color for only a specific line (or word).

DaWei Feb 12th, 2007 8:52 PM

No, I didn't make a post here. People just like to take arbitrary shots at me. I haven't lost as much as one second of sleep over it. The caliber of the detractors is too low. Glad to see you back, Sane.

Dietrich Feb 13th, 2007 12:54 AM

Sorry Sane, that's just ye olde DOS command. Looks like you ought to use wxPython then.

Sane Feb 13th, 2007 8:54 PM

Wow. Are you sure? I've seen in a console window, on Windows, different colors for different lines before. It was written in C.

DaWei Feb 14th, 2007 12:06 AM

Sure, you can write it in C/C++, for Windows. See the Console part of the API. If you want, I can pop you some code, but I thought you wanted to do it in Python, and I haven't researched that. A lot of people say "DOS" when they shouldn't (it's an acronym for Disk Operating System). It means nothing. It could be MSDOS or another OS or the Windows Command facility or the Windows CMD facility or any number of other things.

Dietrich Feb 15th, 2007 12:19 AM

Quote:

Originally Posted by Sane (Post 123916)
Wow. Are you sure? I've seen in a console window, on Windows, different colors for different lines before. It was written in C.

You mean this old thing:
:

#include <stdio.h>
#include <windows.h>

int main(void)
{
  HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  WORD wAttributes;
  CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo ;
 
  // save current text and backgroud colors
  GetConsoleScreenBufferInfo(hConsoleOutput, &lpConsoleScreenBufferInfo);
  wAttributes = lpConsoleScreenBufferInfo.wAttributes;
 
  // set new text and background colors
  SetConsoleTextAttribute(hConsoleOutput, FOREGROUND_RED | FOREGROUND_INTENSITY);
 
  printf("Hello World! in colorful C\n");

  // restore origional text and background colors
  SetConsoleTextAttribute(hConsoleOutput, wAttributes);
 
  getchar();  // wait
  return 0;
}

Sure, you can use the Windows API functions in Python. Just get the Win32 extension package.

Sane Feb 19th, 2007 6:23 PM

Aahh... so those are the win32api functions "MicDareall" was mentioning.

I haven't tried it, but I'm sure it'll work great. Thanks! :)


All times are GMT -5. The time now is 3:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC