I am trying to add syntax highlighting to the text widget in tkinter i am using the code from another stack overflow question Pygments syntax highlighter in python tkinter text widget i binded the function for syntax hyghlighting to the root but the problem is that all the other default binds like CTRL A stops working. They work fine if i bind it to the text widget but the last entered letter doesnt get highlighted. Heres the code(i am new to programming so there might be many silly mistakes)
from tkinter import *
from pygments import lex
from pygments.lexers import PythonLexer
def test(e):
txt.mark_set("range_start", "1.0")
data = txt.get("1.0", "end")
for tag in txt.tag_names():
txt.tag_remove(tag,"1.0","end")
for token, content in lex(data, PythonLexer()):
txt.mark_set("range_end", "range_start + %dc" % len(content))
txt.tag_add(str(token), "range_start", "range_end")
txt.mark_set("range_start", "range_end")
root=Tk()
txt=Text(root)
txt.pack(expand='yes')
txt.tag_configure("Token.Comment.Single", foreground='red')
root.bind('<Any-KeyPress>',test)
root.mainloop()