如何在 python tkinter 的文本小部件中突出显示 python 的整个语法(包括每个模块的关键字、class 等)
How to highlight whole syntax(including every modules's Keywords,class,etc.) of python in text widget in python tkinter
我正在创建代码编辑器,我想正确地突出显示每个代码。我创建了一个代码来突出显示代码
我创建了一个不同的函数来突出显示字符串和注释
但是当我输入任何已经包含一些要突出显示的颜色的代码时,""" 中不显示字符串颜色,但我只希望该代码在 " 之外显示它自己的颜色" 不在里面
例如,如果我在其中键入 from """ 而不是希望该代码显示石灰色,这是字符串的颜色设置我我但它显示青色(我在外面设置 "")
这是我的代码
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()
我正在创建代码编辑器,我想正确地突出显示每个代码。我创建了一个代码来突出显示代码 我创建了一个不同的函数来突出显示字符串和注释 但是当我输入任何已经包含一些要突出显示的颜色的代码时,""" 中不显示字符串颜色,但我只希望该代码在 " 之外显示它自己的颜色" 不在里面 例如,如果我在其中键入 from """ 而不是希望该代码显示石灰色,这是字符串的颜色设置我我但它显示青色(我在外面设置 "") 这是我的代码
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()