如何创建逻辑来为python tkinter中的python shell等特定单词设置颜色
How to create logic to set color for certain word like python shell in python tkinter
enter image description here
如何在 python tkinter 中创建逻辑来为某些单词设置颜色,例如 python shell
例如,如果我输入“导入”,颜色应该是蓝色,其他保持与上图link
中的图像相同
注意:下面的代码基于以下声明 -:
import tkinter as tk
root = tk.Tk()
root.geometry('400x300')
root.title('Widget(With Colored Keywords)')
text = tk.Text(root, background = '#1f1f14', foreground = 'white', insertbackground = 'white')
text.pack()
所需的行为可以使用tkinter中的标签实现 Text
widget,你可以使用正则表达式(pos
和last_pos
)找到单词的位置,然后定义一个基于位置的标签。
如果我们使用文本小部件的 tag_add
方法知道标签的开始和结束位置,我们可以将标签添加到文本小部件。
tkinter.Text.tag_add(tagname, start_pos, end_pos)
注意传递的位置参数必须是Text widget index格式。
此外,我们使用文本小部件的 tag_config
方法来配置标签的属性,在本例中为 foreground
.
此标签随后可用于修改该标签内文本部分的 foreground
颜色 属性。
def check_for_keywords(keyword_dict) :
for i in keyword_dict :
text.tag_remove(i, '1.0', tk.END)
pos = 1.0
while 1:
pattern = r'\m{}\M'.format(i)
pos = text.search(pattern, pos, regexp = True, stopindex = tk.END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i))
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = keyword_dict[i])
root.after(1000, check_for_keywords)
return
给定一个字典,带有{keyword : color_name} 映射,可以创建一个函数来检查关键字。另请注意,必须每秒使用 tkinter window 的 after 方法调用此函数(您可以根据项目的适用性更改检查时间)。
keyword_dict = {'label' : 'red', 'button' : 'blue', 'gml' : 'orange'}
root.after(1000, lambda : check_for_keywords(keyword_dict)) #Checks every one second.
root.mainloop()
完整代码将变为-:
import tkinter as tk
def check_for_keywords(keyword_dict) :
for i in keyword_dict :
text.tag_remove(i, '1.0', tk.END)
pos = 1.0
while 1:
pattern = r'\m{}\M'.format(i)
pos = text.search(pattern, pos, regexp = True, stopindex = tk.END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i))
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = keyword_dict[i])
root.after(1000, lambda : check_for_keywords(keyword_dict))
return
keyword_dict = {'label' : 'red', 'button' : 'blue', 'gml' : 'orange'} #GML CODING KEYWORDS TEST DICT
root = tk.Tk()
root.geometry('400x300')
root.title('Widget(With Colored Keywords)')
text = tk.Text(root, background = '#1f1f14', foreground = 'white', insertbackground = 'white')
text.pack()
root.after(1000, lambda : check_for_keywords(keyword_dict)) #Checks every one second.
root.mainloop()
现在,在键入任何提到的关键字时,关键字会突出显示 -:
注意:
还可以找到更多有用的信息 here.
有关如何处理标签的更多信息,this 也可能是有用的资源。
enter image description here
如何在 python tkinter 中创建逻辑来为某些单词设置颜色,例如 python shell 例如,如果我输入“导入”,颜色应该是蓝色,其他保持与上图link
中的图像相同注意:下面的代码基于以下声明 -:
import tkinter as tk
root = tk.Tk()
root.geometry('400x300')
root.title('Widget(With Colored Keywords)')
text = tk.Text(root, background = '#1f1f14', foreground = 'white', insertbackground = 'white')
text.pack()
所需的行为可以使用tkinter中的标签实现 Text
widget,你可以使用正则表达式(pos
和last_pos
)找到单词的位置,然后定义一个基于位置的标签。
如果我们使用文本小部件的 tag_add
方法知道标签的开始和结束位置,我们可以将标签添加到文本小部件。
tkinter.Text.tag_add(tagname, start_pos, end_pos)
注意传递的位置参数必须是Text widget index格式。
此外,我们使用文本小部件的 tag_config
方法来配置标签的属性,在本例中为 foreground
.
此标签随后可用于修改该标签内文本部分的 foreground
颜色 属性。
def check_for_keywords(keyword_dict) :
for i in keyword_dict :
text.tag_remove(i, '1.0', tk.END)
pos = 1.0
while 1:
pattern = r'\m{}\M'.format(i)
pos = text.search(pattern, pos, regexp = True, stopindex = tk.END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i))
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = keyword_dict[i])
root.after(1000, check_for_keywords)
return
给定一个字典,带有{keyword : color_name} 映射,可以创建一个函数来检查关键字。另请注意,必须每秒使用 tkinter window 的 after 方法调用此函数(您可以根据项目的适用性更改检查时间)。
keyword_dict = {'label' : 'red', 'button' : 'blue', 'gml' : 'orange'}
root.after(1000, lambda : check_for_keywords(keyword_dict)) #Checks every one second.
root.mainloop()
完整代码将变为-:
import tkinter as tk
def check_for_keywords(keyword_dict) :
for i in keyword_dict :
text.tag_remove(i, '1.0', tk.END)
pos = 1.0
while 1:
pattern = r'\m{}\M'.format(i)
pos = text.search(pattern, pos, regexp = True, stopindex = tk.END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i))
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = keyword_dict[i])
root.after(1000, lambda : check_for_keywords(keyword_dict))
return
keyword_dict = {'label' : 'red', 'button' : 'blue', 'gml' : 'orange'} #GML CODING KEYWORDS TEST DICT
root = tk.Tk()
root.geometry('400x300')
root.title('Widget(With Colored Keywords)')
text = tk.Text(root, background = '#1f1f14', foreground = 'white', insertbackground = 'white')
text.pack()
root.after(1000, lambda : check_for_keywords(keyword_dict)) #Checks every one second.
root.mainloop()
现在,在键入任何提到的关键字时,关键字会突出显示 -:
注意: 还可以找到更多有用的信息 here.
有关如何处理标签的更多信息,this 也可能是有用的资源。