python - Tkinter 用背景颜色填充整行

标签 python python-2.7 tkinter

我正在看这个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 列表(参见图片),其中每行颜色交替。使用文本元素可以实现这一点吗?我知道它们是表格,其中交替的行具有不同的颜色,但是因为我有简单的字符串,它们因另一个因素而异,并且认为这将是显示差异的好方法。

Example of iTunes list

最佳答案

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()

enter image description here

关于python - Tkinter 用背景颜色填充整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21674087/

相关文章:

python - 我想向 QVBoxLayout 添加滚动条

python - 在 Python 中使 SWIG 包装内置类 "hashable"

python - 如何为 pandas 数据框行中的每个新分组分配数值?

python - TestDome 联赛表 - Python

python - ML 函数作为 pyspark UDF

python - pyobjc安装失败: xcode-select: error: tool 'xcodebuild' requires Xcode

python - 如何使用多线程执行子进程

python - 无法在条目小部件上设置 focus()

python - 使用剪贴板时如何检查用户右键单击了哪个输入框?

python - 从排序的单词列表中高效打印匹配的 semordnilaps 对