我怎样才能做到,当我在 tkinter 中更改标记的单词时,颜色会变回来



我正在制作一个文本编辑器,我正在使用Pygments来帮助语法突出显示。当我输入一个关键字时,比如for,它会变成黄色,很好。好吧,当我在后面输入一个字母时,比如说forefor仍然是黄色的,或者如果我退格,fof是黄色的。

为什么它不变回黑色/清除颜色?有没有有效的方法可以做到这一点?我的代码,只是为了以防万一,如下所示,没有错误,所以我将只包含突出显示功能,即使它本身可能是不需要的。

如果需要,我会包括更多

import sys #imports
from pygments import highlight
from pygments.lexers import PythonLexer #python syntax highlighter
from pygments import lex
major_version = sys.version_info.major
if major_version == 2: #check python version for importing tkinter
    from Tkinter import *
    import tkFileDialog
elif major_version == 3:
    from tkinter import *
    import tkinter.filedialog as tkFileDialog
else:
    raise RuntimeError('Unexpected python major version: %d' % major_version)
def highlight(t, previousContent):
    content = t.get("1.0", END)
    lines = content.split("n")
    if(content != previousContent):
        t.mark_set("range_start", "1.0")
        data = t.get("1.0", "end-1c")
        for token, content in lex(data, PythonLexer()):
            t.mark_set("range_end", "range_start + %dc" % len(content))
            t.tag_add(str(token), "range_start", "range_end")
            t.mark_set("range_start", "range_end")
def highlightLine(t, previousContent):
    content = t.get("1.0", END)
    lines = content.split("n")
    currentCursorPosition = t.index(INSERT)
    currentCursorPositionSplit = currentCursorPosition.split(".")
    currentLine = currentCursorPositionSplit[0]
    currentColumn = currentCursorPositionSplit[1]
    if(content != previousContent):
        t.mark_set("range_start", str(currentLine) + ".0")
        data = t.get(str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
        for token, content in lex(data, PythonLexer()):
            t.mark_set("range_end", "range_start + %dc" % len(content))
            t.tag_add(str(token), "range_start", "range_end")
            t.mark_set("range_start", "range_end")
def initHighlight(t):
    t.tag_configure("Token.Keyword", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Constant", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Declaration", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Namespace", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Pseudo", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Reserved", foreground="#CC7A00")
    t.tag_configure("Token.Keyword.Type", foreground="#CC7A00")
    t.tag_configure("Token.Name.Class", foreground="#003D99")
    t.tag_configure("Token.Name.Exception", foreground="#003D99")
    t.tag_configure("Token.Name.Function", foreground="#003D99")
    t.tag_configure("Token.Operator.Word", foreground="#CC7A00")
    t.tag_configure("Token.Comment", foreground="#B80000")
    t.tag_configure("Token.Literal.String", foreground="#248F24")

这个问题不是问如何从文本中删除所有标签,而是问我如何检查被标记的单词是否不再是理想的单词,然后删除该单词上的标签。如果我要删除所有标签然后重新标记所有内容,那就太滞后了。

在标记之前,在同一函数中,我添加了

for tag in t.tag_names():
    t.tag_remove(tag, str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))

它检查所有标签,然后删除当前行中的标签,然后,它将重新添加它们,完整功能如下:

def highlightLine(t, previousContent):
    content = t.get("1.0", END)
    lines = content.split("n")
    currentCursorPosition = t.index(INSERT)
    currentCursorPositionSplit = currentCursorPosition.split(".")
    currentLine = currentCursorPositionSplit[0]
    currentColumn = currentCursorPositionSplit[1]
    if(content != previousContent):
        t.mark_set("range_start", str(currentLine) + ".0")
        data = t.get(str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
        for tag in t.tag_names():
            t.tag_remove(tag, str(currentLine) + ".0", str(currentLine) + "." + str(currentColumn))
        for token, content in lex(data, PythonLexer()):
            t.mark_set("range_end", "range_start + %dc" % len(content))
            t.tag_add(str(token), "range_start", "range_end")
            t.mark_set("range_start", "range_end")

现在完美运行!

最新更新