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()
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()