Home > other >  How can I change the color of certain characters using tkinter in python?
How can I change the color of certain characters using tkinter in python?

Time:05-09

I'm coding a brainfuck compiler and im having trouble updating the text color, as I want different symbols to have a different color depending on what they are. I am currently using this code but I'm having trouble when trying to change the color of both '[' and ']' symbols.

colormap = {']': '#a94926', ' ': '#cc7832', '-': '#cc7832', '<': '#6a8759', '>': '#6a8759', ',': '#6396ba',
        '.': '#6396ba', '[': '#a94926'}

def on_key(event):
if event.char in colormap:
    event.widget.tag_add(event.char, 'insert-1c')

editor.pack(expand=True, fill=BOTH, padx=(1.75, 2.5), pady=(2.5, 1.75))
for c in colormap:
    editor.tag_config(c, foreground=colormap[c])

editor.bind('<KeyRelease>', on_key)

That's the code for the editor where the user codes, but when the user writes [ or ] it does not color it.

CodePudding user response:

I was able to get this working by using a ScrolledText widget for editor FWIW, ttk.Entry widgets don't seem to support tag_add or tag_config

import tkinter as tk
from tkinter.scrolledtext import ScrolledText

class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('App')
        self.geometry('300x400')
        self.editor = ScrolledText(self)
        self.editor.pack(
            expand=True,
            fill=tk.BOTH,
            padx=(1.75, 2.5),
            pady=(2.5, 1.75),
        )
        self.colormap = {
            ']': '#a94926',
            ' ': '#cc7832',
            '-': '#cc7832',
            '<': '#6a8759',
            '>': '#6a8759',
            ',': '#6396ba',
            '.': '#6396ba',
            '[': '#a94926',
        }
        for c in self.colormap:
            self.editor.tag_config(c, foreground=self.colormap[c])
    
        self.editor.bind('<KeyRelease>', self.on_key)

    def on_key(self, event):
        if event.char in self.colormap:
            event.widget.tag_add(event.char, 'insert-1c')

if __name__ == '__main__':
    app = App()
    app.mainloop()

CodePudding user response:

Found a solution for my issue, ended up using pygments package to divide text into different BF characters. I created this two new functions:

def colorFormat(event):
    code = editor.get('1.0', 'end-1c')
    i = 1
    for line in code.splitlines():
        editor.index("%d.0" % i)
        formatLine(line=i)
        editor.update()
        i  = 1


def formatLine(line=None):
    start_range = 0
    index = editor.index('insert').split('.')

    if line is None:
        line = int(index[0])

    line_text = editor.get("{}.{}".format(line, 0), "{}.end".format(line))

    for token, content in lex(line_text, BrainfuckLexer()):
        end_range = start_range   len(content)
        keySet = content[0]
        if keySet in colormap:
        editor.tag_add(keySet, '{}.{}'.format(line, start_range), '{}.{}'.format(line, end_range))
        start_range = end_range

And called colorFormat every time a key press is detected with:

editor.bind('<KeyRelease>', colorFormat)

With these two functions, I can also callcolorFormat each time user opens a new file.

  • Related