ThriceGood
ThriceGood

Reputation: 1703

Python3: Cannot reset colors with Colorama module

ive been trying to use colorama to print a dictionary value in a color.

i want to have a user input select the value to be colored and reset the colors of the previous selection when the user picks a differnt value, ive been able to change the color based on the selection be a can figure out how to reset the previous selections.

in the code below ive recreated the basic idea, you will see in the 'else' block some of the things ive tried to reset the colors, but they did not work.

im using Python 3.4 and 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)

any ideas?

ANSWER

i was able to work a solution, although i couldn't get it to work in the situation above, it works in the real situation i needed it for. It doesnt really make sense without knowing whats in the allBoards dict but the rowString is the important part.

        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

Upvotes: 2

Views: 7447

Answers (2)

Rydex
Rydex

Reputation: 420

You can try:

print('\033[39m')

Upvotes: 1

falsetru
falsetru

Reputation: 369394

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

According to above line, GREEN, RESET_ALL keep prepend/appended.

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

You need to remove the surround GREEN, RESET_ALL in else block.

Instead of doing that, how about doing as follow with simple 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)

Upvotes: 2

Related Questions