用背景色填充整行

2024-05-02 20:31:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看这个example用背景色填充文本小部件。不过,我希望整个行的背景色显示,而不仅仅是有文本的地方。从这个例子中我可以说:

text.tag_add("here", "1.0", "1.80")
text.tag_config("here", background="yellow", foreground="blue")

这是因为标准文本小部件的宽度为80个字符。这也没用:

^{pr2}$

这两者都有同样的不良影响。我试图达到的效果与iTunes列表(见图片)类似,其中每一行的颜色交替。使用文本元素可以实现这一点吗?我知道它们是表格,交替的行是不同的颜色,但我有简单的字符串,在另一个因素上有所不同,我认为这将是一个很好的方式来显示差异。在

Example of iTunes list


Tags: text文本addconfighere颜色部件example
1条回答
网友
1楼 · 发布于 2024-05-02 20:31:15

指定line_number + 1.0endindex。在

更新

指定line_number.0+1linesendindex。 (参见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()

enter image description here

相关问题 更多 >