Python3:无法使用Colorama模块重置颜色

时间:2015-10-18 14:33:45

标签: python python-3.x terminal python-3.4 colorama

我一直在尝试用colorama打印颜色的字典值。

我希望有一个用户输入选择要着色的值,并在用户选择不同的值时重置上一个选择的颜色,我已经能够根据选择更改颜色了一个可以弄清楚如何重置之前的选择。

在下面的代码中我重新创建了基本的想法,你会在'else'块中看到我尝试重置颜色的一些东西,但它们没有用。

即时使用Python 3.4和colorama 0.3.3

import colorama
from colorama import Fore, Back, Style
from msvcrt import getch

colorama.init()

a1 = 'a1 text'
a2 = 'a2 text'
a3 = 'a3 text'
a4 = 'a4 text'

aDict = {49: a1, 50: a2, 51: a3, 52: a4}

choice = 0

# while choice is not the letter 'q'
while choice != 113:

    print('select number betweem 1 and 4:\n')
    # read key
    choice = ord(getch())

    # loop through dict of text
    for k, v in aDict.items():
        # if the key is equal to the choice
        if k == choice:
            # set the color
            aDict[k] = Fore.GREEN + v + Style.RESET_ALL
        else:
            # else reset the color
            # aDict[k] = v + Style.RESET_ALL
            # aDict[k] = Fore.RESET + v
            # print(Style.RESET_ALL)
            pass

    # print the text
    for k, v in aDict.items():
        print(v)

任何想法?

ANSWER

我能够找到一个解决方案,虽然我无法让它在上面的情况下工作,它可以在我需要它的真实情况下工作。如果不知道allBoards dict中的什么是真的有意义,但rowString是重要的部分。

        for boards in allBoards:
            for i in range(1, 4):
            for number, board in boards.items():                                
                    if number == currentBoard:  
                        # rowString += Back.WHITE + Fore.BLACK + board[i][1] +'|'+ board[i][2] +'|'+ board[i][3] + Back.RESET + Fore.RESET + ' | '                          
                        rowString += Fore.CYAN + board[i][1] +'|'+ board[i][2] +'|'+ board[i][3] + Fore.RESET + ' | '                           
                    elif number in wins['X']:
                        rowString += Fore.RED + board[i][1] +'|'+ board[i][2] +'|'+ board[i][3] + Fore.RESET + ' | '
                    elif number in wins['O']:
                        rowString += Fore.GREEN + board[i][1] +'|'+ board[i][2] +'|'+ board[i][3] + Fore.RESET + ' | '
                    else:
                        rowString += board[i][1] +'|'+ board[i][2] +'|'+ board[i][3] + ' | '        
                rowStrings.append(rowString[:-3])
                rowString = ''

        i = 1
        for string in rowStrings:
            print(string)
            if i % 3 == 0 and i != 9:
                print('---------------------')
            i += 1

2 个答案:

答案 0 :(得分:2)

aDict[k] = Fore.GREEN + v + Style.RESET_ALL

根据上面的行,GREEN,RESET_ALL保持前置/附加。

  • GREEN a1 text RESET_ALL
  • GREEN GREEN a1 text RESET_ALL RESET_ALL
  • GREEN GREEN GREEN a1 text RESET_ALL RESET_ALL RESET_ALL

您需要删除GREEN块中的环绕RESET_ALLelse

而不是这样做,如何按照简单if .. else ..

进行操作
while choice != 113:
    print('select number betweem 1 and 4:\n')
    choice = ord(getch())
    for k, v in aDict.items():
        if k == choice:
            print(Fore.GREEN + v + Style.RESET_ALL)
        else:
            print(v)

答案 1 :(得分:0)

你可以试试:

print('\033[39m')