Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter用背景色填充整行_Python_Python 2.7_Tkinter - Fatal编程技术网

Python Tkinter用背景色填充整行

Python Tkinter用背景色填充整行,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我在用背景色填充文本小部件。但是,我希望背景颜色显示为整个行,而不仅仅是在有文本的地方。从这个例子中,我可以说: 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", back

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

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


行号+1
0
指定为
endindex

更新

行号
.0+1行
指定为
endindex
。 (参见,尤其是表达部分)

最简单的例子:

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()
试试看:
从tkinter进口*
除恐怖外:
从Tkinter进口*
text=text()
text.pack()
text.insert(结束'1st\n2nd\n3rd')

#text.tag_add(“此处”、“2.0”、“3.0”)#我没有想到增加行号,因为我认为行号也会改变该行的背景颜色。@Dean,我找到了比手动添加1更好的方法。我更新了答案。过来看。