我正在看这个example用于用背景颜色填充文本小部件。不过,我希望整行显示背景颜色,而不仅仅是有文本的地方。所以从这个例子中我可以说:
text.tag_add("here", "1.0", "1.80")
text.tag_config("here", background="yellow", foreground="blue")
这是因为标准文本小部件的宽度为 80 个字符。这也不起作用:
text.tag_add("here", "1.0", 1.END)
text.tag_config("here", background="yellow", foreground="blue")
这两者都有同样的不良影响。我想要实现的效果类似于 iTunes 列表(参见图片),其中每行颜色交替。使用文本元素可以实现这一点吗?我知道它们是表格,其中交替的行具有不同的颜色,但是因为我有简单的字符串,它们因另一个因素而异,并且认为这将是显示差异的好方法。
最佳答案
将 line_number + 1
.
0
指定为 endindex
。
更新
将 line_number
.0+1lines
指定为 endindex
。
(参见 The Tkinter Text Widget documentation ,特别是表达式部分)
最小示例:
try:
from tkinter import *
except ImportError:
from Tkinter import *
text = Text()
text.pack()
text.insert(END, '1st\n2nd\n3rd')
# text.tag_add("here", "2.0", "3.0") # <----------------------
text.tag_add("here", "2.0", "2.0+1lines") # <----------------------
text.tag_config("here", background="yellow", foreground="blue")
mainloop()
关于python - Tkinter 用背景颜色填充整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21674087/