පයිතන්හි පර්යන්තයට වර්ණ පෙළ ප්රතිදානය කරන්නේ කෙසේද?
█   ගැටළුව වන්නේ එය ASCII දිගු කිරීම පමණි, සමහර විට ඔබට එය භාවිතයෙන් වැඩ කර ගත හැකියhttp://stackoverflow.com/questions/8465226/using-extended-ascii-codes-with-python
                පයිතන්හි පර්යන්තයට වර්ණ පෙළ ප්රතිදානය කරන්නේ කෙසේද?
█   ගැටළුව වන්නේ එය ASCII දිගු කිරීම පමණි, සමහර විට ඔබට එය භාවිතයෙන් වැඩ කර ගත හැකියhttp://stackoverflow.com/questions/8465226/using-extended-ascii-codes-with-python
                Answers:
මෙය තරමක් රඳා පවතින්නේ ඔබ සිටින්නේ කුමන වේදිකාව මතද යන්න මතය. මෙය කිරීමට වඩාත් පොදු ක්රමය වන්නේ ANSI ගැලවීමේ අනුපිළිවෙල මුද්රණය කිරීමයි. සරල උදාහරණයක් සඳහා, බ්ලෙන්ඩර් බිල්ඩ් ස්ක්රිප්ට් වලින් පයිතන් කේත කිහිපයක් මෙන්න :
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'මේ වගේ කේත භාවිතා කිරීමට, ඔබට එවැනි දෙයක් කළ හැකිය
print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)හෝ, Python3.6 + සමඟ:
print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")මෙය OS X, ලිනක්ස් සහ වින්ඩෝස් ඇතුළු යුනික්ස් මත ක්රියා කරනු ඇත (ඔබ ANSICON භාවිතා කරන්නේ නම් හෝ වින්ඩෝස් 10 හි VT100 අනුකරණයන් සක්රීය කර තිබේ නම් ). වර්ණය සැකසීම, කර්සරය චලනය කිරීම සහ තවත් බොහෝ දේ සඳහා ඇන්සි කේත ඇත.
ඔබ මෙය සමඟ සංකීර්ණ වීමට යන්නේ නම් (ඔබ ක්රීඩාවක් ලියන්නේ නම් එය ඔබ මෙන් පෙනේ), ඔබ "ශාප" මොඩියුලය පිළිබඳව සොයා බැලිය යුතුය, මෙය ඔබ වෙනුවෙන් සංකීර්ණ කොටස් රාශියක් හසුරුවයි. මෙම පිඹුරා සාප ස්ථාපන හොඳ හඳුන්වා ඇත.
ඔබ දීර් AS ASCII භාවිතා නොකරන්නේ නම් (එනම් පරිගණකයක නොවේ), ඔබ 127 ට අඩු ascii අක්ෂර සමඟ සිරවී සිටින අතර, '#' හෝ '@' බොහෝ විට වාරණයක් සඳහා ඔබේ හොඳම ඔට්ටුව විය හැකිය. ඔබේ පර්යන්තය IBM විස්තාරිත ascii අක්ෂර කට්ටලයක් භාවිතා කරන බව ඔබට සහතික කළ හැකි නම් , ඔබට තවත් බොහෝ විකල්ප තිබේ. 176, 177, 178 සහ 219 අක්ෂර "බ්ලොක් අක්ෂර" වේ.
"වාමන බලකොටුව" වැනි සමහර නවීන පෙළ පදනම් කරගත් වැඩසටහන්, පෙළ ප්රකාරය චිත්රක ආකාරයෙන් අනුකරණය කරන අතර සම්භාව්ය පරිගණක අකුරු වල රූප භාවිතා කරයි. ඔබට වාමන බලකොටුව විකී බලන්න ( පරිශීලක-සාදන ලද ටයිල්සෙට් ) භාවිතා කළ හැකි මෙම බිට්මැප් කිහිපයක් ඔබට සොයාගත හැකිය .
මෙම පාඨමය Demo තරගය පාඨමය රූප සඳහා ද වැඩි සම්පත් ඇත.
හ්ම් .. මම හිතන්නේ මේ පිළිතුරෙන් ටිකක් away ත් වෙලා. මම වීර කාව්ය පාදක ත්රාසජනක ක්රීඩාවක් සැලසුම් කරමින් සිටිමි. ඔබේ වර්ණ පා text යට සුබ පැතුම්!
tput, එසේ වගේ එය වඩා එහා මෙහා ගෙන යා හැකි කේතය ප්රතිඵලයක් සිට.
                    disableඔබ ප්රතිදානය ගොනුවකට නල කරන විට සැබෑ භාවිත අවස්ථාවකි ; වැනි මෙවලම් catවර්ණවලට සහය විය හැකි නමුත් සාමාන්යයෙන් වර්ණ තොරතුරු ලිපිගොනු වලට මුද්රණය නොකිරීම වඩා හොඳය.
                    import ctypes; kernel32 = ctypes.WinDLL('kernel32'); hStdOut = kernel32.GetStdHandle(-11); mode = ctypes.c_ulong(); kernel32.GetConsoleMode(hStdOut, ctypes.byref(mode)); mode.value |= 4; kernel32.SetConsoleMode(hStdOut, mode).
                    පයිතන් ටර්ම්කොලර් මොඩියුලය කිසිවෙකු සඳහන් නොකිරීම ගැන මට පුදුමයි . භාවිතය ඉතා සරල ය:
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')හෝ පයිතන් 3 හි:
print(colored('hello', 'red'), colored('world', 'green'))එය ප්රමාණවත් තරම් නවීන නොවිය හැක, කෙසේ වෙතත්, ක්රීඩා වැඩසටහන් සහ ඔබට කිරීමට අවශ්ය “වර්ණ කුට්ටි” සඳහා ...
termcolor.COLORSඔබට වර්ණ ලැයිස්තුවක් ලබා දෙයි
                    os.system('color')පළමුව ධාවනය වන විට , පසුව ANSI ගැලවීමේ අනුක්රමය ක්රියාත්මක වීමට පටන් ගනී.
                    පිලිතුර වන්නේ, Colorama Python සියලු හරස් වේදිකා වර්ණ සඳහා.
<b>bold</b>පර්යන්තය පෙළ ශෛලිය එක් කිරීම සඳහා)
                    වර්ණයක් / විලාසිතාවක් ආරම්භ කරන නූලක් මුද්රණය කරන්න, ඉන්පසු නූල්, ඉන්පසු වර්ණය / විලාසිතාවේ වෙනස අවසන් කරන්න '\x1b[0m':
print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')පහත කේතය සමඟ ෂෙල් පෙළ සඳහා ආකෘති විකල්ප වගුවක් ලබා ගන්න:
def print_format_table():
    """
    prints table of formatted text format options
    """
    for style in range(8):
        for fg in range(30,38):
            s1 = ''
            for bg in range(40,48):
                format = ';'.join([str(style), str(fg), str(bg)])
                s1 += '\x1b[%sm %s \x1b[0m' % (format, format)
            print(s1)
        print('\n')
print_format_table()
වර්ණයක් ආරම්භ කරන නූලක් සහ වර්ණය අවසන් කරන නූලක් නිර්වචනය කරන්න, ඉන්පසු ඔබේ පෙළ ඉදිරිපස ආරම්භක නූලෙන් සහ අවසානයේ කෙළවරේ මුද්රණය කරන්න.
CRED = '\033[91m'
CEND = '\033[0m'
print(CRED + "Error, does not compute!" + CEND)මේ පහත සඳහන් නිෂ්පාදනය bashතුළ, urxvtඑය Zenburn ආකාරයේ වර්ණ සමග:
අත්හදා බැලීම් තුළින් අපට තවත් වර්ණ ලබා ගත හැකිය:
සටහන: \33[5mසහ \33[6mදිලිසෙනවා.
මේ ආකාරයෙන් අපට සම්පූර්ණ වර්ණ එකතුවක් නිර්මාණය කළ හැකිය:
CEND      = '\33[0m'
CBOLD     = '\33[1m'
CITALIC   = '\33[3m'
CURL      = '\33[4m'
CBLINK    = '\33[5m'
CBLINK2   = '\33[6m'
CSELECTED = '\33[7m'
CBLACK  = '\33[30m'
CRED    = '\33[31m'
CGREEN  = '\33[32m'
CYELLOW = '\33[33m'
CBLUE   = '\33[34m'
CVIOLET = '\33[35m'
CBEIGE  = '\33[36m'
CWHITE  = '\33[37m'
CBLACKBG  = '\33[40m'
CREDBG    = '\33[41m'
CGREENBG  = '\33[42m'
CYELLOWBG = '\33[43m'
CBLUEBG   = '\33[44m'
CVIOLETBG = '\33[45m'
CBEIGEBG  = '\33[46m'
CWHITEBG  = '\33[47m'
CGREY    = '\33[90m'
CRED2    = '\33[91m'
CGREEN2  = '\33[92m'
CYELLOW2 = '\33[93m'
CBLUE2   = '\33[94m'
CVIOLET2 = '\33[95m'
CBEIGE2  = '\33[96m'
CWHITE2  = '\33[97m'
CGREYBG    = '\33[100m'
CREDBG2    = '\33[101m'
CGREENBG2  = '\33[102m'
CYELLOWBG2 = '\33[103m'
CBLUEBG2   = '\33[104m'
CVIOLETBG2 = '\33[105m'
CBEIGEBG2  = '\33[106m'
CWHITEBG2  = '\33[107m'පරීක්ෂණය ජනනය කිරීම සඳහා කේතය මෙන්න:
x = 0
for i in range(24):
  colors = ""
  for j in range(5):
    code = str(x+j)
    colors = colors + "\33[" + code + "m\\33[" + code + "m\033[0m "
  print(colors)
  x=x+5\33[0mහෝ ඊට CENDඉහළින්.
                    ඔබට ANSI ගැලවීමේ අනුපිළිවෙල ගැන ඉගෙන ගැනීමට අවශ්යය. මෙන්න කෙටි උදාහරණයක්:
CSI="\x1B["
print(CSI+"31;40m" + "Colored Text" + CSI + "0m")වැඩි විස්තර සඳහා http://en.wikipedia.org/wiki/ANSI_escape_code බලන්න
බ්ලොක් අක්ෂරයක් සඳහා, ic u2588 වැනි යුනිකෝඩ් අක්ෂරයක් උත්සාහ කරන්න:
print(u"\u2588")එකට ඒ සියල්ල දමා:
print(CSI+"31;40m" + u"\u2588" + CSI + "0m")def d(*v): return '\x1B['+';'.join(map(str, v))+'m'එහෙනම් උත්සාහ කරන්නprint ' '.join([d(k,i)+str(i%10)+d(0) for i in range(30,38)+range(40,48) for k in range(2)])
                    මම ප්රතිචාර දක්වන්නේ වින්ඩෝස් 10 හි ANSI කේත භාවිතා කිරීමට ක්රමයක් සොයාගෙන ඇති නිසාය, එවිට ඔබට කිසිදු මොඩියුලයක් නොමැතිව පෙළෙහි වර්ණය වෙනස් කළ හැකිය:
මෙම කාර්යය සිදු කරන රේඛාව os.system(""), හෝ වෙනත් ඕනෑම පද්ධති ඇමතුමක් වන අතර එමඟින් ටර්මිනලයේ ANSI කේත මුද්රණය කිරීමට ඉඩ ලබා දේ:
import os
os.system("")
# Group of Different functions for different styles
class style():
    BLACK = '\033[30m'
    RED = '\033[31m'
    GREEN = '\033[32m'
    YELLOW = '\033[33m'
    BLUE = '\033[34m'
    MAGENTA = '\033[35m'
    CYAN = '\033[36m'
    WHITE = '\033[37m'
    UNDERLINE = '\033[4m'
    RESET = '\033[0m'
print(style.YELLOW + "Hello, World!")සටහන: මෙය වෙනත් වින්ඩෝස් විකල්පයන්ට සමාන විකල්පයන් ලබා දුන්නද, වින්ඩෝස් මෙම උපක්රමය සමඟ වුවද ANSI කේත සඳහා පූර්ණ සහය නොදක්වයි. සියලුම පෙළ සැරසිලි වර්ණ ක්රියා නොකරන අතර සියලු 'දීප්තිමත්' වර්ණ (කේත 90-97 සහ 100-107) සාමාන්ය වර්ණවලට සමාන වේ (කේත 30-37 සහ 40-47)
සංස්කරණය කරන්න : ඊටත් වඩා කෙටි ක්රමයක් සොයා ගැනීම ගැන @jl ට ස්තූතියි.
tl; dr : os.system("")ඔබේ ගොනුවේ ඉහළට එක් කරන්න.
පයිතන් අනුවාදය: 3.6.7
if sys.platform.lower() == "win32": os.system('color')එය සාධාරණ ලෙස ප්රතිස්ථාපනය කිරීමෙන් ඔබට තවත් සරල කළ හැකි බව මම සොයා ගතිමි os.system(''). කිසිදු කොන්දේසියක් අවශ්ය නොවන අතර කේතය වින්ඩෝස් 10 සහ ලිනක්ස් යන දෙකෙහිම ක්රියාත්මක වේ (මා එය පරීක්ෂා කළ විට). ඔබට පෙනෙන පරිදි, ඔබට පද්ධති ඇමතුමක් ගැනීමට අවශ්ය නැත color. ඇමතුම් dir, cd, abcdef,, සාධාරණ හිස් අගයක් වැඩ දඩ (මෙම, හිස්-නොවන නූල් ඉඩ ඔබ දැකීමට කැමති නැහැ ප්රතිදානය මුද්රණය වුවත්).
                    colorතීරණාත්මක කොටස නොවේ; එය තියෙන්නේ os.system(command)පවා හුදෙක් හිස් අගයක් - Windows 10. තවද "විධාන" මත ධාවනය වන විට හැකි වර්ණ මුද්රණය දෙයක්, ඇත්තටම විය හැකි කරවන මාර්ගය ම.
                    මගේ ප්රියතම ක්රමය වන්නේ ආශිර්වාද පුස්තකාලයයි (සම්පූර්ණ අනාවරණය: මම එය ලිව්වා). උදාහරණයක් වශයෙන්:
from blessings import Terminal
t = Terminal()
print t.red('This is red.')
print t.bold_bright_red_on_black('Bright red on black')වර්ණ ගඩොල් මුද්රණය කිරීම සඳහා වඩාත් විශ්වාසදායක ක්රමය වන්නේ පසුබිම් වර්ණ සහිත අවකාශයන් මුද්රණය කිරීමයි. නාසය-ප්රගතිශීලී ලෙස ප්රගති තීරුව ඇඳීමට මම මෙම තාක්ෂණය භාවිතා කරමි :
print t.on_green(' ')ඔබට නිශ්චිත ස්ථානවලද මුද්රණය කළ හැකිය:
with t.location(0, 5):
    print t.on_yellow(' ')ඔබගේ ක්රීඩාවේදී ඔබට වෙනත් පර්යන්ත හැකියාවන් සමඟ සම්බන්ධ වීමට සිදුවුවහොත්, ඔබට එයද කළ හැකිය. ඔබට කියවිය හැකි පරිදි තබා ගැනීමට ඔබට පයිතන්ගේ සම්මත නූල් හැඩතල ගැන්වීම භාවිතා කළ හැකිය:
print '{t.clear_eol}You just cleared a {t.bold}whole{t.normal} line!'.format(t=t)ආශිර්වාදයේ ඇති හොඳ දෙය නම්, එය (අධික ලෙස පොදු) ANSI- වර්ණ පමණක් නොව, සියලු වර්ගවල පර්යන්තවල වැඩ කිරීමට උපරිම උත්සාහයක් දැරීමයි. භාවිතයට සංක්ෂිප්තව සිටියදී එය කියවිය නොහැකි ගැලවීමේ අනුක්රමයන් ඔබේ කේතයෙන් බැහැර කරයි. විනෝද වන්න!
getattr. හෝ බොහෝ විට, ඒ වෙනුවට ගතිකව ආකෘති නූල් සාදන්න.
                    can just passපයිතන් ශ්රිතයක්.
                    sty colorama සමාන වන නමුත්, එය අඩු විස්ථරාත්මක තියෙන්නේ, ආධාරක 8bit හා 24bit (RGB) වර්ණ, ආධාරක muting, ඔබේ ම මෝස්තර ලියාපදිංචි කිරීමට ඔබ ඉඩ ලබා දෙන අතර, හොඳින් වාර්තා හා වඩා, ඇත්තටම නම්යශීලී වේ.
උදාහරණ:
from sty import fg, bg, ef, rs
foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs
# Add custom colors:
from sty import Style, RgbFg
fg.orange = Style(RgbFg(255, 150, 50))
buf = fg.orange + 'Yay, Im orange.' + fg.rs
print(foo, bar, baz, qux, qui, buf, sep='\n')මුද්රණ:
100 දක්වා වූ සෑම වර්ණ සංයෝජනයක්ම පුනරාවර්තනය කිරීම සඳහා ෆෝ ලූපයක් භාවිතා කරමින් සියලු වර්ණ සහිත පන්තියක් ජනනය කර, පසුව පයිතන් වර්ණ සහිත පන්තියක් ලිවීය. GPLv2 මා විසින් පිටපත් කර අලවන්න:
class colors:
    '''Colors class:
    reset all colors with colors.reset
    two subclasses fg for foreground and bg for background.
    use as colors.subclass.colorname.
    i.e. colors.fg.red or colors.bg.green
    also, the generic bold, disable, underline, reverse, strikethrough,
    and invisible work with the main class
    i.e. colors.bold
    '''
    reset='\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    class fg:
        black='\033[30m'
        red='\033[31m'
        green='\033[32m'
        orange='\033[33m'
        blue='\033[34m'
        purple='\033[35m'
        cyan='\033[36m'
        lightgrey='\033[37m'
        darkgrey='\033[90m'
        lightred='\033[91m'
        lightgreen='\033[92m'
        yellow='\033[93m'
        lightblue='\033[94m'
        pink='\033[95m'
        lightcyan='\033[96m'
    class bg:
        black='\033[40m'
        red='\033[41m'
        green='\033[42m'
        orange='\033[43m'
        blue='\033[44m'
        purple='\033[45m'
        cyan='\033[46m'
        lightgrey='\033[47m'මෙම සරල කේතය උත්සාහ කරන්න
def prRed(prt): print("\033[91m {}\033[00m" .format(prt))
def prGreen(prt): print("\033[92m {}\033[00m" .format(prt))
def prYellow(prt): print("\033[93m {}\033[00m" .format(prt))
def prLightPurple(prt): print("\033[94m {}\033[00m" .format(prt))
def prPurple(prt): print("\033[95m {}\033[00m" .format(prt))
def prCyan(prt): print("\033[96m {}\033[00m" .format(prt))
def prLightGray(prt): print("\033[97m {}\033[00m" .format(prt))
def prBlack(prt): print("\033[98m {}\033[00m" .format(prt))
prGreen("Hello world")Win32 API වෙත ප්රවේශ වීම සඳහා වින්ඩෝස් හි ඔබට මොඩියුලය 'win32console' (සමහර පයිතන් බෙදාහැරීම් වලින් ලබා ගත හැකිය) හෝ මොඩියුලය 'ctypes' (Python 2.5 සහ ඉහළ) භාවිතා කළ හැකිය.
ක්රම දෙකටම සහය දක්වන සම්පූර්ණ කේතය බැලීමට, ටෙස්ටූබ් වෙතින් වර්ණ කොන්සෝලය වාර්තා කිරීමේ කේතය බලන්න .
ctypes උදාහරණය:
import ctypes
# Constants from the Windows API
STD_OUTPUT_HANDLE = -11
FOREGROUND_RED    = 0x0004 # text color contains red.
def get_csbi_attributes(handle):
    # Based on IPython's winconsole.py, written by Alexander Belchenko
    import struct
    csbi = ctypes.create_string_buffer(22)
    res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(handle, csbi)
    assert res
    (bufx, bufy, curx, cury, wattr,
    left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
    return wattr
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
reset = get_csbi_attributes(handle)
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, FOREGROUND_RED)
print "Cherry on top"
ctypes.windll.kernel32.SetConsoleTextAttribute(handle, reset)මගේ කේතයේ ඕනෑම තැනක භාවිතා කළ හැකි ගෝලීය කාර්යයන් සහිත මොඩියුලයකට මම @ ජොල්ඩ් පිළිතුර ඔතා ඇත.
ගොනුව: log.py
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = "\033[1m"
def disable():
    HEADER = ''
    OKBLUE = ''
    OKGREEN = ''
    WARNING = ''
    FAIL = ''
    ENDC = ''
def infog( msg):
    print OKGREEN + msg + ENDC
def info( msg):
    print OKBLUE + msg + ENDC
def warn( msg):
    print WARNING + msg + ENDC
def err( msg):
    print FAIL + msg + ENDCපහත පරිදි භාවිතා කරන්න:
 import log
    log.info("Hello World")
    log.err("System Error")@ ජොයෙල්ඩ්ගේ පිළිතුර මත පදනම්ව මෝඩ සරල ය
class PrintInColor:
    RED = '\033[91m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    LIGHT_PURPLE = '\033[94m'
    PURPLE = '\033[95m'
    END = '\033[0m'
    @classmethod
    def red(cls, s, **kwargs):
        print(cls.RED + s + cls.END, **kwargs)
    @classmethod
    def green(cls, s, **kwargs):
        print(cls.GREEN + s + cls.END, **kwargs)
    @classmethod
    def yellow(cls, s, **kwargs):
        print(cls.YELLOW + s + cls.END, **kwargs)
    @classmethod
    def lightPurple(cls, s, **kwargs):
        print(cls.LIGHT_PURPLE + s + cls.END, **kwargs)
    @classmethod
    def purple(cls, s, **kwargs):
        print(cls.PURPLE + s + cls.END, **kwargs)එහෙනම්
PrintInColor.red('hello', end=' ')
PrintInColor.green('world')printස්ථානගත කිරීම් ද ? විරෝධය අවලංගු කරන ලදි.
                    printප්රතිස්ථාපනය කිරීම සඳහා එහි ක්රියාකාරිත්වය නිසි ලෙස ප්රතිවර්තනය කිරීමට වග බලා ගත යුතුය.
                    def purple(cls, *args, **kwargs):     print(cls.PURPLE, *args, cls.END, **kwargs)</code>
                    ඔබ win32api භාවිතා නොකරන්නේ නම් වින්ඩෝස් සඳහා ඔබට වර්ණ සමඟ සැනසීමට මුද්රණය කළ නොහැක.
ලිනක්ස් සඳහා එය මුද්රණය භාවිතා කිරීම තරම්ම සරල ය, ගැලවීමේ අනුපිළිවෙල මෙහි දක්වා ඇත:
චරිතය කොටුවක් මෙන් මුද්රණය කිරීම සඳහා, එය සැබවින්ම රඳා පවතින්නේ ඔබ කොන්සෝල කවුළුව සඳහා භාවිතා කරන අකුරු මතය. පවුම් සංකේතය හොඳින් ක්රියා කරයි, නමුත් එය අකුරු මත රඳා පවතී:
## Pure Python 3.x demo, 256 colors
# Works with bash under Linux and MacOS
fg = lambda text, color: "\33[38;5;" + str(color) + "m" + text + "\33[0m"
bg = lambda text, color: "\33[48;5;" + str(color) + "m" + text + "\33[0m"
def print_six(row, format, end="\n"):
    for col in range(6):
        color = row*6 + col - 2
        if color>=0:
            text = "{:3d}".format(color)
            print (format(text,color), end=" ")
        else:
            print(end="    ")   # four spaces
    print(end=end)
for row in range(0, 43):
    print_six(row, fg, " ")
    print_six(row, bg)
# Simple usage: print(fg("text", 160))def black(text):
    print('\033[30m', text, '\033[0m', sep='')
def red(text):
    print('\033[31m', text, '\033[0m', sep='')
def green(text):
    print('\033[32m', text, '\033[0m', sep='')
def yellow(text):
    print('\033[33m', text, '\033[0m', sep='')
def blue(text):
    print('\033[34m', text, '\033[0m', sep='')
def magenta(text):
    print('\033[35m', text, '\033[0m', sep='')
def cyan(text):
    print('\033[36m', text, '\033[0m', sep='')
def gray(text):
    print('\033[90m', text, '\033[0m', sep='')
black("BLACK")
red("RED")
green("GREEN")
yellow("YELLOW")
blue("BLACK")
magenta("MAGENTA")
cyan("CYAN")
gray("GRAY")මම මෙය කිරීම අවසන් කළෙමි, එය පිරිසිදු බව මට හැඟුණි:
formatters = {             
    'RED': '\033[91m',     
    'GREEN': '\033[92m',   
    'END': '\033[0m',      
}
print 'Master is currently {RED}red{END}!'.format(**formatters)
print 'Help make master {GREEN}green{END} again!'.format(**formatters)Https://pypi.python.org/pypi/lazyme   භාවිතා කරමින් @joeld පිළිතුර මත ගොඩනැගීම pip install -U lazyme:
from lazyme.string import color_print
>>> color_print('abc')
abc
>>> color_print('abc', color='pink')
abc
>>> color_print('abc', color='red')
abc
>>> color_print('abc', color='yellow')
abc
>>> color_print('abc', color='green')
abc
>>> color_print('abc', color='blue', underline=True)
abc
>>> color_print('abc', color='blue', underline=True, bold=True)
abc
>>> color_print('abc', color='pink', underline=True, bold=True)
abcතිර රුව:
color_printනව ආකෘති සමඟ සමහර යාවත්කාලීන කිරීම් , උදා:
>>> from lazyme.string import palette, highlighter, formatter
>>> from lazyme.string import color_print
>>> palette.keys() # Available colors.
['pink', 'yellow', 'cyan', 'magenta', 'blue', 'gray', 'default', 'black', 'green', 'white', 'red']
>>> highlighter.keys() # Available highlights.
['blue', 'pink', 'gray', 'black', 'yellow', 'cyan', 'green', 'magenta', 'white', 'red']
>>> formatter.keys() # Available formatter, 
['hide', 'bold', 'italic', 'default', 'fast_blinking', 'faint', 'strikethrough', 'underline', 'blinking', 'reverse']සටහන : italic, fast blinkingසහ strikethroughසියලු පර්යන්තවල ක්රියා නොකරනු ඇත, මැක් / උබුන්ටු මත ක්රියා නොකරයි.
උදා
>>> color_print('foo bar', color='pink', highlight='white')
foo bar
>>> color_print('foo bar', color='pink', highlight='white', reverse=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', bold=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', faint=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', faint=True, reverse=True)
foo bar
>>> color_print('foo bar', color='pink', highlight='white', underline=True, reverse=True)
foo barතිර රුව:
withනැවත සැකසිය යුතු (පයිතන් 3 සහ කොලරාමා භාවිතා කරමින්) මෙවැනි විකරණකාරක සමඟ මූල පදය කෙතරම් හොඳින් මිශ්ර වේද යන්න සැලකිල්ලට ගන්න :
from colorama import Fore, Style
import sys
class Highlight:
  def __init__(self, clazz, color):
    self.color = color
    self.clazz = clazz
  def __enter__(self):
    print(self.color, end="")
  def __exit__(self, type, value, traceback):
    if self.clazz == Fore:
      print(Fore.RESET, end="")
    else:
      assert self.clazz == Style
      print(Style.RESET_ALL, end="")
    sys.stdout.flush()
with Highlight(Fore, Fore.GREEN):
  print("this is highlighted")
print("this is not")print(Style.BRIGHT + "Header Test")සහ print (Style.DIM + word)ඇත්තටම නියමයි කඩිනම් නිර්මාණය කරන්න.
                    contextlibPy3 සඳහා භාවිතා කිරීමට මෙය වෙනස් කිරීමට අවශ්ය වනු ඇත .
                    @contextlib.contextmanagerමත සැරසිලි කරුවෙකු සිටිය යුතුය, නැත?
                    ඔබට ශාප පුස්තකාලයේ පයිතන් ක්රියාත්මක කිරීම භාවිතා කළ හැකිය: http://docs.python.org/library/curses.html
එසේම, මෙය ක්රියාත්මක කරන්න, එවිට ඔබට ඔබේ කොටුව සොයාගත හැකිය:
for i in range(255):
    print i, chr(i)ඔබට CLINT භාවිතා කළ හැකිය:
from clint.textui import colored
print colored.red('some warning message')
print colored.green('nicely done!')මම පරක්කු බව මම දනිමි. නමුත්, මට ColorIt නමින් පුස්තකාලයක් ඇත. එය ඉතා සරල ය.
මෙන්න උදාහරණ කිහිපයක්:
from ColorIt import *
# Use this to ensure that ColorIt will be usable by certain command line interfaces
initColorIt()
# Foreground
print (color ('This text is red', colors.RED))
print (color ('This text is orange', colors.ORANGE))
print (color ('This text is yellow', colors.YELLOW))
print (color ('This text is green', colors.GREEN))
print (color ('This text is blue', colors.BLUE))
print (color ('This text is purple', colors.PURPLE))
print (color ('This text is white', colors.WHITE))
# Background
print (background ('This text has a background that is red', colors.RED))
print (background ('This text has a background that is orange', colors.ORANGE))
print (background ('This text has a background that is yellow', colors.YELLOW))
print (background ('This text has a background that is green', colors.GREEN))
print (background ('This text has a background that is blue', colors.BLUE))
print (background ('This text has a background that is purple', colors.PURPLE))
print (background ('This text has a background that is white', colors.WHITE))
# Custom
print (color ("This color has a custom grey text color", (150, 150, 150))
print (background ("This color has a custom grey background", (150, 150, 150))
# Combination
print (background (color ("This text is blue with a white background", colors.BLUE), colors.WHITE))
මෙය ඔබට ලබා දෙයි:
මෙය හරස් වේදිකාවක් වන අතර මැක්, ලිනක්ස් සහ කවුළු මත පරීක්ෂා කර ඇති බව ද සඳහන් කිරීම වටී.
ඔබට එය අත්හදා බැලීමට අවශ්ය විය හැකිය: https://github.com/CodeForeverAndEver/ColorIt
සටහන: දිලිසීම, ඇල අකුරු, තද අකුරු ආදිය දින කිහිපයකින් එකතු වේ.
ඔබ ක්රීඩාවක් වැඩසටහන්ගත කරන්නේ නම් පසුබිම් වර්ණය වෙනස් කර අවකාශ පමණක් භාවිතා කිරීමට ඔබ කැමතිද? උදාහරණයක් වශයෙන්:
print " "+ "\033[01;41m" + " " +"\033[01;46m"  + "  " + "\033[01;42m"ඔබ වින්ඩෝස් භාවිතා කරන්නේ නම්, මෙන්න ඔබ යන්න!
# display text on a Windows console
# Windows XP with Python27 or Python32
from ctypes import windll
# needed for Python2/Python3 diff
try:
    input = raw_input
except:
    pass
STD_OUTPUT_HANDLE = -11
stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
# look at the output and select the color you want
# for instance hex E is yellow on black
# hex 1E is yellow on blue
# hex 2E is yellow on green and so on
for color in range(0, 75):
     windll.kernel32.SetConsoleTextAttribute(stdout_handle, color)
     print("%X --> %s" % (color, "Have a fine day!"))
     input("Press Enter to go on ... ")print("%X --> %s" % (color, "Have a fine day!"), end='', flush=True)
                    ඔබ ජැන්ගෝ භාවිතා කරන්නේ නම්
>>> from django.utils.termcolors import colorize
>>> print colorize("Hello World!", fg="blue", bg='red',
...                 opts=('bold', 'blink', 'underscore',))
Hello World!
>>> help(colorize)ඡායාරූපය:

(මම සාමාන්යයෙන් ධාවක පර්යන්තයේ නිදොස්කරණය සඳහා වර්ණ ප්රතිදානය භාවිතා කරමි, එබැවින් මම එය එකතු කළෙමි.)
එය ඔබේ යන්ත්රයේ 
 $ python -c "import django; print django.VERSION"
ස්ථාපනය කර ඇත්දැයි ඔබට පරීක්ෂා කළ හැකිය: එය ස්ථාපනය කිරීමට පරීක්ෂා කරන්න: ජැන්ගෝ ස්ථාපනය කරන්නේ කෙසේද
එය උත්සාහ කර බලන්න !!
මෙන්න ශාප උදාහරණයක්:
import curses
def main(stdscr):
    stdscr.clear()
    if curses.has_colors():
        for i in xrange(1, curses.COLORS):
            curses.init_pair(i, i, curses.COLOR_BLACK)
            stdscr.addstr("COLOR %d! " % i, curses.color_pair(i))
            stdscr.addstr("BOLD! ", curses.color_pair(i) | curses.A_BOLD)
            stdscr.addstr("STANDOUT! ", curses.color_pair(i) | curses.A_STANDOUT)
            stdscr.addstr("UNDERLINE! ", curses.color_pair(i) | curses.A_UNDERLINE)
            stdscr.addstr("BLINK! ", curses.color_pair(i) | curses.A_BLINK)
            stdscr.addstr("DIM! ", curses.color_pair(i) | curses.A_DIM)
            stdscr.addstr("REVERSE! ", curses.color_pair(i) | curses.A_REVERSE)
    stdscr.refresh()
    stdscr.getch()
if __name__ == '__main__':
    print "init..."
    curses.wrapper(main)https://raw.github.com/fabric/fabric/master/fabric/colors.py
"""
.. versionadded:: 0.9.2
Functions for wrapping strings in ANSI color codes.
Each function within this module returns the input string ``text``, wrapped
with ANSI color codes for the appropriate color.
For example, to print some text as green on supporting terminals::
    from fabric.colors import green
    print(green("This text is green!"))
Because these functions simply return modified strings, you can nest them::
    from fabric.colors import red, green
    print(red("This sentence is red, except for " + \
          green("these words, which are green") + "."))
If ``bold`` is set to ``True``, the ANSI flag for bolding will be flipped on
for that particular invocation, which usually shows up as a bold or brighter
version of the original color on most terminals.
"""
def _wrap_with(code):
    def inner(text, bold=False):
        c = code
        if bold:
            c = "1;%s" % c
        return "\033[%sm%s\033[0m" % (c, text)
    return inner
red = _wrap_with('31')
green = _wrap_with('32')
yellow = _wrap_with('33')
blue = _wrap_with('34')
magenta = _wrap_with('35')
cyan = _wrap_with('36')
white = _wrap_with('37')පෙළ UI සහ සජීවිකරණ ගොඩනැගීම සඳහා asciimatics විසින් අතේ ගෙන යා හැකි ආධාරකයක් සපයයි:
#!/usr/bin/env python
from asciimatics.effects import RandomNoise  # $ pip install asciimatics
from asciimatics.renderers import SpeechBubble, Rainbow
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
def demo(screen):
    render = Rainbow(screen, SpeechBubble('Rainbow'))
    effects = [RandomNoise(screen, signal=render)]
    screen.play([Scene(effects, -1)], stop_on_resize=True)
while True:
    try:
        Screen.wrapper(demo)
        break
    except ResizeScreenError:
        passAsciicast:
පයිතන් 3 මුද්රණ ශ්රිතය ආවරණය කරන තවත් පයිපී මොඩියුලයක්:
https://pypi.python.org/pypi/colorprint
ඔබත් නම් එය python 2.x හි භාවිතා කළ හැකිය from __future__ import print. මොඩියුල පයිපි පිටුවෙන් පයිතන් 2 උදාහරණයක් මෙන්න:
from __future__ import print_function
from colorprint import *
print('Hello', 'world', color='blue', end='', sep=', ')
print('!', color='red', format=['bold', 'blink'])නිමැවුම් "හෙලෝ, ලෝකය!" නිල් පැහැයෙන් සහ විශ්මයජනක සලකුණ තද රතු සහ දිලිසෙන සලකුණු වලින්.