0

I am creating a small game in the console, using PrettyTable, but I can not find information on how to color the table. I know that you can make a color table in Tkinter, but I need it exactly without using it

Here is a piece:

def draw_board():
    from prettytable import PrettyTable
    for i in range(5):
        x = PrettyTable()
        x.field_names = ["", "", "", "", ""]
        x.add_row([1, 2, 3, 4, 5])
        x.add_row([6, 7, 8, 9, 10])
        x.add_row([11, 12, 13, 14, 15])
        x.add_row([16, 17, 18, 19, 20])
        x.add_row([21, 22, 23, 24, 25])
    print(x)
Ayush Gupta
  • 1,166
  • 2
  • 9
  • 25
  • 1
    Please take a moment to properly format your code samples. See [editing help](https://stackoverflow.com/editing-help) for more information. – larsks Aug 07 '21 at 15:01
  • You may be able to kind of do this by putting [ANSI color escape sequences](https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences) if your console supports them, but they may throw-off the character width calculations PrettyTables does internally. – martineau Aug 07 '21 at 15:23

1 Answers1

1

maybe this helps you:

from prettytable import PrettyTable 

#Color
R = "\033[0;31;40m" #RED
G = "\033[0;32;40m" # GREEN
Y = "\033[0;33;40m" # Yellow
B = "\033[0;34;40m" # Blue
N = "\033[0m" # Reset

color = ["\033[0;31;40m", "\033[0;32;40m", "\033[0;33;40m", "\033[0;34;40m", "\033[0m"]

def draw_board(): 

    x = PrettyTable() 
    x.field_names = ["c1", "c2", "c3", "c4", "c5"] 
    for i in [1,6,11,16,21]:
        l = list()
        for j in range(5):
            l.append(color[j]+str(i+j)+N)
        x.add_row(l) 

    print(x)
    
draw_board()

output:

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30