[英]Change specific character color in Tkinter.Canvas
I'm trying to change specific characters color in Tkinter.Canvas... I search on net for a solution and nothing. 我正在尝试在Tkinter.Canvas中更改特定字符的颜色...我在网上搜索解决方案,但一无所获。
txt1 = canvasFrame.create_text(500,100, text = "redblue", font = "Calibri, 30", fill = "#4587de")
The actual color is blue, I need to paint the 'red string' with red color and the 'blue string' still blue. 实际的颜色是蓝色,我需要将“红色字符串”涂成红色,并将“蓝色字符串”涂成蓝色。
There is a way to do this? 有办法吗?
The canvas doesn't allow to use multiple colors for one string object. 画布不允许对一个字符串对象使用多种颜色。 You will need to use two
create_text
commands, one for each color. 您将需要使用两个
create_text
命令,每种颜色一个。 That also means you'll have to calculate the proper spacing and alignment of the two strings on the canvas so that it looks like one string. 这也意味着您必须计算画布上两个字符串的正确间距和对齐方式,以使其看起来像一个字符串。
The canvas isn't a very good solution if you need to color individual characters. 如果您需要为单个字符上色,则画布不是一个很好的解决方案。 If you can, you might want to switch to using a text widget.
如果可以,您可能要切换为使用文本小部件。
One can put a text widget (with colored text) on a canvas. 可以将文本小部件(带有彩色文本)放在画布上。
from tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()
text = Text(canvas, height=1, width=20)
text.insert('end', 'red green blue')
text['state'] = DISABLED # read only
canvas.create_window(100, 20, anchor='nw', window=text)
root.mainloop()
There are other SO answers on coloring slices within a text by using tags. 通过使用标记,在文本的着色切片上还有其他SO答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.