【问题标题】:Tkinter fill whole line with background colourTkinter 用背景颜色填充整行
【发布时间】:2014-02-10 10:03:35
【问题描述】:

我正在查看 example 以使用背景颜色填充 Text 小部件。但是我希望背景颜色出现在整行中,而不仅仅是在有文本的地方。所以从这个例子中我可以说:

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

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    line_number + 1.0指定为endindex

    更新

    line_number .0+1lines 指定为endindex。 (见The Tkinter Text Widget documentation,尤其是Expressions部分)

    小例子:

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

    【讨论】:

    • 我没想过要增加行号,因为我认为它也会改变该行的背景颜色。
    • @Dean,我找到了比手动添加 1 更好的方法。我更新了答案。看看吧。
    猜你喜欢
    • 2014-08-28
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    相关资源
    最近更新 更多