1

I am creating code editor, I want to highlight every code properly. I have created a code to highlight code and I have created a different function to highlight string and comments but when I type any code from which already contain some color to highlight doesn't show string color inside "" but I want only that code show it's own color outside "" not inside for example if I type from inside it "" than want that code show lime color which is color of string set my me but it show cyan(set by me for outside "") this is my code

from tkinter import*
def Keys_word(Keys_list):
    for i in Keys_list:
        editor.tag_remove(i,"1.0",END)
        pos = 1.0
        while True:
            pattern = r"\m{}\M".format(i)
            pos = editor.search(pattern,pos,regexp=True,stopindex=END)
            if not pos:
                break
            last_pos = "%s+%sc"%(pos,len(i))
            editor.tag_add(i,pos,last_pos)
            pos = last_pos
        editor.tag_configure(i,foreground=Keys_list[i])
    root.after(1000,lambda:Keys_word(Keys_list))
    Custom_highlight_for_string()
    return
def Custom_highlight_for_string():
        myCount = IntVar()
        regex_pattern = [r'".*"',r'#.*',r"'''.*'''",r"'.*'"]
        for pattern in regex_pattern:
            editor.mark_set("start","1.0")
            editor.mark_set("end",END)
            num = int(regex_pattern.index(pattern))
            while True:
                index = editor.search(pattern,"start","end",count=myCount,regexp=True)
                if index == "": break
                if num == 0:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                elif num == 1:
                    editor.tag_add(color[1],index,index +" lineend")
                elif num == 2:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                elif num == 3:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                editor.mark_set("start","%s+%sc"%(index,myCount.get()))
            
root = Tk()
Keys_list = {"def":"blue","from":"cyan","import":"cyan","class":'orange'}
color = ["lime","red"]
editor = Text(font="Consolas 11")
editor.tag_configure("lime",foreground="lime")
editor.tag_configure("red",foreground="red")
editor.pack()

root.after(1000,lambda:Keys_word(Keys_list))




root.mainloop()

This is what I have created enter image description here

Coder
  • 92
  • 11
  • it seems you will have to create some parser because using your regex you can't resolve it. OR maybe you could use pygment for this. You may also check source code of IDE [thonny](https://thonny.org/) which also use `tkinter`. – furas Mar 20 '22 at 09:14
  • [ast](https://docs.python.org/3/library/ast.html) is your friend. – Richard Neumann Mar 20 '22 at 21:36

1 Answers1

0

I have found this solution this solution solve my problem

from tkinter import*
def Keys_word(Keys_list):
    for i in Keys_list:
        editor.tag_remove(i,"1.0",END)
        pos = 1.0
        while True:
            pattern = r"\m{}\M".format(i)
            pos = editor.search(pattern,pos,regexp=True,stopindex=END)
            if not pos:
                break
            last_pos = "%s+%sc"%(pos,len(i))
            editor.tag_add(i,pos,last_pos)
            pos = last_pos
        # editor.tag_configure(i,foreground=Keys_list[i])
    root.after(1000,lambda:Keys_word(Keys_list))
    Custom_highlight_for_string()
    return
def Custom_highlight_for_string():
        myCount = IntVar()
        regex_pattern = [r'".*"',r'#.*',r"'''.*'''",r"'.*'"]
        for pattern in regex_pattern:
            editor.mark_set("start","1.0")
            editor.mark_set("end",END)
            num = int(regex_pattern.index(pattern))
            while True:
                index = editor.search(pattern,"start","end",count=myCount,regexp=True)
                if index == "": break
                if num == 0:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                elif num == 1:
                    editor.tag_add(color[1],index,index +" lineend")
                elif num == 2:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                elif num == 3:
                    editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
                editor.mark_set("start","%s+%sc"%(index,myCount.get()))
            
root = Tk()
Keys_list = {"def":"blue","from":"cyan","import":"cyan","class":'orange'}
color = ["lime","red"]
editor = Text(font="Consolas 11")
for i in Keys_list:
    editor.tag_configure(i,foreground=Keys_list[i])
editor.tag_configure("lime",foreground="lime")
editor.tag_configure("red",foreground="red")
editor.pack()

root.after(1000,lambda:Keys_word(Keys_list))




root.mainloop()
Coder
  • 92
  • 11
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 22 '22 at 09:40