如何在 python tkinter 中为 *、# 等符号设置自定义颜色
如何为某些符号设置特定颜色,例如 、# 等' 例如,如果我输入“”,它的颜色应该是蓝色,其他保持不变。
typedecker 先生,我正在绑定您这样的功能,但这不起作用
from tkinter import*
root = Tk()
def check_for_symbols(symbol_dict) :
for i in symbol_dict :
text.tag_remove(i, '1.0', END)
pos = 1.0
while 1:
pos = text.search(i, pos, regexp = True, stopindex = END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i)) # The only change
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = symbol_dict[i])
root.after(1000, lambda:check_for_symbols(symbol_dict))
return
symbol_dict = {
"*":"blue"
}
text = Text(root, background = "gray19", foreground = "white", insertbackground = 'white',font="Consolas 15 italic")
text.pack(expand=True,fill=BOTH)
root.after(1000, lambda : check_for_symbols(symbol_dict))
root.mainloop()
How to set specific color for certain symbols like ,#,etc '
example if I type "" it's color should be blue and other stay remain same.
typedecker sir i am binding you function like this but this is not working
from tkinter import*
root = Tk()
def check_for_symbols(symbol_dict) :
for i in symbol_dict :
text.tag_remove(i, '1.0', END)
pos = 1.0
while 1:
pos = text.search(i, pos, regexp = True, stopindex = END)
if not pos:
break
last_pos = '%s+%dc' % (pos, len(i)) # The only change
text.tag_add(i, pos, last_pos)
pos = last_pos
text.tag_config(i, foreground = symbol_dict[i])
root.after(1000, lambda:check_for_symbols(symbol_dict))
return
symbol_dict = {
"*":"blue"
}
text = Text(root, background = "gray19", foreground = "white", insertbackground = 'white',font="Consolas 15 italic")
text.pack(expand=True,fill=BOTH)
root.after(1000, lambda : check_for_symbols(symbol_dict))
root.mainloop()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与单词情况相同的过程 这里是我的,可以遵循,唯一的变化是用于检测的正则表达式符号。
进行了必要更改的完整
check_for_symbols
函数将如下所示:The same procedure as followed in the case of words here by me, can be followed, the only change will be in the regex expression that is to be used to detect the symbols.
The full
check_for_symbols
function with the necessary changes in place will look like -: