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
JavaScript
x
52
52
1
from tkinter import*
2
def Keys_word(Keys_list):
3
for i in Keys_list:
4
editor.tag_remove(i,"1.0",END)
5
pos = 1.0
6
while True:
7
pattern = r"m{}M".format(i)
8
pos = editor.search(pattern,pos,regexp=True,stopindex=END)
9
if not pos:
10
break
11
last_pos = "%s+%sc"%(pos,len(i))
12
editor.tag_add(i,pos,last_pos)
13
pos = last_pos
14
editor.tag_configure(i,foreground=Keys_list[i])
15
root.after(1000,lambda:Keys_word(Keys_list))
16
Custom_highlight_for_string()
17
return
18
def Custom_highlight_for_string():
19
myCount = IntVar()
20
regex_pattern = [r'".*"',r'#.*',r"'''.*'''",r"'.*'"]
21
for pattern in regex_pattern:
22
editor.mark_set("start","1.0")
23
editor.mark_set("end",END)
24
num = int(regex_pattern.index(pattern))
25
while True:
26
index = editor.search(pattern,"start","end",count=myCount,regexp=True)
27
if index == "": break
28
if num == 0:
29
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
30
elif num == 1:
31
editor.tag_add(color[1],index,index +" lineend")
32
elif num == 2:
33
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
34
elif num == 3:
35
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
36
editor.mark_set("start","%s+%sc"%(index,myCount.get()))
37
38
root = Tk()
39
Keys_list = {"def":"blue","from":"cyan","import":"cyan","class":'orange'}
40
color = ["lime","red"]
41
editor = Text(font="Consolas 11")
42
editor.tag_configure("lime",foreground="lime")
43
editor.tag_configure("red",foreground="red")
44
editor.pack()
45
46
root.after(1000,lambda:Keys_word(Keys_list))
47
48
49
50
51
root.mainloop()
52
Advertisement
Answer
I have found this solution this solution solve my problem
JavaScript
1
54
54
1
from tkinter import*
2
def Keys_word(Keys_list):
3
for i in Keys_list:
4
editor.tag_remove(i,"1.0",END)
5
pos = 1.0
6
while True:
7
pattern = r"m{}M".format(i)
8
pos = editor.search(pattern,pos,regexp=True,stopindex=END)
9
if not pos:
10
break
11
last_pos = "%s+%sc"%(pos,len(i))
12
editor.tag_add(i,pos,last_pos)
13
pos = last_pos
14
# editor.tag_configure(i,foreground=Keys_list[i])
15
root.after(1000,lambda:Keys_word(Keys_list))
16
Custom_highlight_for_string()
17
return
18
def Custom_highlight_for_string():
19
myCount = IntVar()
20
regex_pattern = [r'".*"',r'#.*',r"'''.*'''",r"'.*'"]
21
for pattern in regex_pattern:
22
editor.mark_set("start","1.0")
23
editor.mark_set("end",END)
24
num = int(regex_pattern.index(pattern))
25
while True:
26
index = editor.search(pattern,"start","end",count=myCount,regexp=True)
27
if index == "": break
28
if num == 0:
29
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
30
elif num == 1:
31
editor.tag_add(color[1],index,index +" lineend")
32
elif num == 2:
33
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
34
elif num == 3:
35
editor.tag_add(color[0],index,"%s+%sc"%(index,myCount.get()) )
36
editor.mark_set("start","%s+%sc"%(index,myCount.get()))
37
38
root = Tk()
39
Keys_list = {"def":"blue","from":"cyan","import":"cyan","class":'orange'}
40
color = ["lime","red"]
41
editor = Text(font="Consolas 11")
42
for i in Keys_list:
43
editor.tag_configure(i,foreground=Keys_list[i])
44
editor.tag_configure("lime",foreground="lime")
45
editor.tag_configure("red",foreground="red")
46
editor.pack()
47
48
root.after(1000,lambda:Keys_word(Keys_list))
49
50
51
52
53
root.mainloop()
54