I'm very new to python and programming in general. I'm working on a tiny and ill-advised minigame involving a heatmap of space, using a text box in tkinter (if I weren't a week into this I would just use a different tool). Here's a video of what that looks like: video
To recolor the text this way, I'm 1: building an array of text indexes for each heatmap value (the navy isn't currently used because I realized I can just make it the default text color but I've left it in)
#scans through each "pixel" in the viewport - translates values to the text I want displayed and records the index in the appropriate color group
for y in range(20):
for x in range(26):
pix=viewarray[y][(x*2)-1]
if pix != "/" and pix != "a" and pix != "b" and pix != "c" and pix != "d" and pix != "e" and pix != "f" and int(pix) >= 16:
pix = "/"
pix = pix.replace("10","a")
pix = pix.replace("11", "b")
pix = pix.replace("12", "c")
pix = pix.replace("13", "d")
pix = pix.replace("14", "e")
pix = pix.replace("15", "f")
viewarray[y][(x * 2) - 1] = pix
# if pix == "0" or pix=="1" or pix=="2" or pix=="3":
# navygroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "4" or pix =="5":
bluegroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "6" or pix =="7":
cyangroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "8" or pix =="9":
greengroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "a" or pix =="b":
yellowgroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "c" or pix =="d":
orangegroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "e" or pix =="f":
redgroup.append(str(y+1)+"."+str((x*2)-1))
if pix == "/":
red4group.append(str(y+1)+"."+str((x*2)-1))
Then, inside the updating loop, tag_add and tag_configure are applied for each group:
self.colortagger(tagname="bgroup", tagcolor="blue", array=bluegroup)
self.colortagger(tagname="cgroup", tagcolor="cyan", array=cyangroup)
self.colortagger(tagname="ggroup", tagcolor="#00FF00", array=greengroup)
self.colortagger(tagname="ygroup", tagcolor="yellow", array=yellowgroup)
self.colortagger(tagname="ogroup", tagcolor="#FFAA00", array=orangegroup)
self.colortagger(tagname="rgroup", tagcolor="red", array=redgroup)
self.colortagger(tagname="r4group", tagcolor="red4", array=red4group)
and elsewhere:
def colortagger(self,tagname,tagcolor,array):
global viewport
for i in range(len(array)):
self.viewport.tag_add(tagname, array[i])
array.clear()
self.viewport.tag_configure(tagname, foreground=tagcolor)
As seen in the video I shared, this slows down terribly if a lot of characters have to be recolored at once - it was even slower when all the navy characters were being recolored instead of just setting it as the default color - so I think that it must be something to do with how tag_add and tag_configure work? (there are a lot of for loops that scan through each individual character but none of them seem to slow it down this severely)
Is there a way to speed up this coloring? Or an alternate way to color select characters in a tkinter text box?