我正在創建代碼編輯器,我想正確突出顯示每個代碼。我創建了一個代碼來突出顯示代碼,并且我創建了一個不同的函式來突出顯示字串和注釋,但是當我鍵入任何已經包含要突出顯示的顏色的代碼時,不會在""內顯示字串顏色,但我只希望顯示該代碼它是自己的顏色外部“”而不是內部例如如果我從內部鍵入“”而不是希望該代碼顯示石灰顏色,這是字串的顏色設定我但它顯示青色(由我設定為外部“”)這是我的代碼
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()
這就是我創建的

uj5u.com熱心網友回復:
我找到了這個解決方案這個解決方案解決了我的問題
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446461.html
