본문 바로가기
프로그램

[파이썬] DOS Print Color 지정

by 오디세이99 2022. 8. 6.
728x90
반응형

파이썬을 '명령 프롬프트' 상에서 많이 사용하면서 Text에 Color를 사용 할 수 있습니다.

 

 

STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12


FOREGROUND_BLACK = 0x00
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN = 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN = 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.


import ctypes

std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)


def set_color(color, handle=std_out_handle):
   bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
   return bool


for i in range(0, 16):
   set_color(i)
   print("Hello, world!")

set_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) # 원래모드

 

 

마지막에 아래 코드를 넣어주어야만 합니다. 그렇지 않으면 계속 Color가 유지 됩니다.

set_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

 

728x90
반응형

댓글