Я создаю редактор кода, я хочу правильно выделить каждый код. Я создал код для выделения кода и я создал другую функцию для выделения строки и комментариев но когда я набираю любой код, из которого уже содержится некоторый цвет для выделения, цвет строки не отображается внутри "", но я хочу, чтобы только этот код показывал свой собственный цвет снаружи "", а не внутри например, если я наберу от внутри него "", чем хочу, чтобы этот код отображал цвет лайма, который является цветом строки, установленной мной, но показывает голубой (установленный мной для внешнего "") это мой код
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()
Я нашел это решение это решение решает мою проблему
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()
Как сейчас написано, ваш ответ неясен. Пожалуйста, редактировать, чтобы добавить дополнительную информацию, которая поможет другим понять, как это относится к заданному вопросу. Дополнительную информацию о том, как писать хорошие ответы, можно найти в справочном центре.
кажется, вам придется создать парсер, потому что с помощью вашего регулярного выражения вы не можете его разрешить. ИЛИ, может быть, вы могли бы использовать pygment для этого. Вы также можете проверить исходный код IDE тонкий, который также использует
tkinter
.