简体   繁体   English

如何在文本小部件Tkinter中突出显示最后添加的文本

[英]How to highlight last added text in text widget tkinter

I want to highlight a last added text in my text widget. 我想在文本小部件中突出显示最后添加的文本。 I have seen an example regarding that How to highlight text in a tkinter Text widget .The problem is that I add a text with "\\n" . 我看到了一个有关如何在tkinter文本小部件中突出显示文本的示例。问题是我在文本中添加了"\\n" That's why program consider current line as a new line so it highlights the empty line. 这就是程序将当前行视为新行,从而突出显示空行的原因。

Do you have any idea how I can alter the program? 您是否知道如何更改程序? Here is my code 这是我的代码

import time
import tkinter as tk
from threading import Thread


class MyApp:

def __init__(self, master):
    self.master = master
    self.text = tk.Text(self.master)
    self.text.pack(side="top", fill="both", expand=True)
    self.text.tag_configure("current_line", background="#e9e9e9")
    self.start_adding_text()
    self._highlight_current_line()


def start_adding_text(self):
    thrd1 = Thread(target=self.add_tex)
    thrd1.start()

def add_tex(self):
    text = "This is demo text\n"
    for _ in range(20):
        self.text.insert(tk.END, text)
        time.sleep(0.1)
    return

def _highlight_current_line(self, interval=100):
    '''Updates the 'current line' highlighting every "interval" milliseconds'''
    self.text.tag_remove("current_line", 1.0, "end")
    self.text.tag_add("current_line", "insert linestart", "insert lineend+1c")
    self.master.after(interval, self._highlight_current_line)


if __name__ == '__main__':
   root = tk.Tk()
   app = MyApp(master=root)
   root.mainloop()

Your function _highlight_current_line is doing what it is supposed to do: it highlights the line of the insert-cursor. 您的函数_highlight_current_line正在执行应做的工作:突出显示插入光标的行。 But what you want is to highlight the last inserted text which is something different. 但是,您想要突出显示最后插入的文本,这有所不同。 You can simply create a new tag. 您可以简单地创建一个新标签。 Let's name it 'last_insert' : 我们将其命名为'last_insert'

self.text.tag_configure("last_insert", background="#e9e9e9")

And when you add text, you can specifiy the tag(s) attached to the inserted text: 并且在添加文本时,您可以指定附加到插入文本的标签:

self.text.insert(tk.END, text, ('last_insert',))

Of course, if you want only the last inserted text to be highlighted, you add this: 当然,如果只希望突出显示最后插入的文本,则添加以下内容:

self.text.tag_remove("last_insert", 1.0, "end")

Remark: The tkinter function tag_add takes as arguments tag , start , end , where start and end are text indices in the form of a string 'a.b' where a is the line index (starting with 1 at the top) and b is the character inside this line (starting with 0). 备注: tag_add函数tag_addtagstartend用作参数,其中startend是字符串'a.b'形式的文本索引,其中a是行索引(以1开头), b是字符在此行内(从0开始)。 You can modify the index with expressions (see here: http://effbot.org/tkinterbook/text.htm . Further, "insert" is a mark (read up on aforementioned link) - and "insert linestart" is replaced by tkinter by the index "line.0" where line is the line the insert cursor is currently in. 您可以使用表达式修改索引(请参见此处: http : //effbot.org/tkinterbook/text.htm 。此外,“插入”是一个标记(在上述链接上有读)-并且"insert linestart""insert linestart"代替按索引"line.0" ,其中line是插入光标当前所在的line

You could check if you are at the last line and remove your newline: 您可以检查自己是否在最后一行,并删除换行符:

def add_tex(self):
    loop_times=20
    text = "This is demo text\n"
    for id,_ in enumerate(list(range(loop_times))):
        if id==loop_times-1:
            text = "This is demo text"
        self.text.insert(tk.END, text)
        time.sleep(0.1)
    return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM