Tkinter / Pygments:奇怪的结果突出显示

时间:2018-06-10 18:01:38

标签: python tkinter pygments

我尝试根据令牌突出显示文本。代码可以通过以下代码突出显示,但有一些不需要的结果。一些例子:

假设,我打字:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- from pygments import lex from pygments.token import Token from pygments.lexers import Python3Lexer try: import Tkinter as tk except ImportError: import tkinter as tk ROOT = tk.Tk() TEXT = tk.Text(master=ROOT, fg="white", bg="black", font="TkDefaultFont 10") TEXT.pack(fill="both", expand=True) def tag(event): def colorize(word, color): index = [] index1 = TEXT.search(word, "1.0", "end") while index1: index2 = ".".join([index1.split(".")[0], str(int(index1.split(".")[1]) + len(word))]) index.append((index1, index2)) index1 = TEXT.search(word, index2, "end") for i, j in index: TEXT.tag_add(word, i, j) TEXT.tag_configure(word, foreground=color) for token, content in lex(TEXT.get("1.0", "end"), Python3Lexer()): if token == Token.Literal.Number.Integer: colorize(content, color="purple") elif token == Token.Keyword: colorize(content, color="orange") elif token == Token.Operator.Word: colorize(content, color="red") elif token == Token.Name.Builtin: colorize(content, color="blue") elif token == Token.Comment.Hashbang or token == Token.Comment.Single: colorize(content, color="grey") elif token == Token.Keyword.Namespace: colorize(content, color="yellow") elif token == Token.Namespace: colorize(content, color="green") elif token == Token.Punctuation: colorize(content, color="brown") elif token == Token.Literal.String.Double: colorize(content, color="cyan") elif token == Token.Name: colorize(content, color="white") TEXT.bind("<KeyRelease>", tag) ROOT.mainloop()

尽管第一个'a'是 Token.Name ,但是第二个'a'是 Token.Literal.String.Double <,两个a都以相同的颜色突出显示/ p>

另一个不需要的情况是,当我键入“i​​f”时,单词会突出显示,如果我继续在单词“if”中添加一些字母,则单词的颜色会按预期更改。但是,当我删除该单词的某些字母,直到该单词再次成为“if”时,此单词不会像以前那样突出显示。

你能帮我理解这个问题吗?

代码:

{{1}}

`

1 个答案:

答案 0 :(得分:0)

问题通过以下代码解决。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
if sys.version_info.major == 2:
    exit()
elif sys.version_info.major == 3:
    import builtins as builtins
    import tkinter as tk
import io
import tokenize
import keyword


root = tk.Tk()
text = tk.Text(master=root, fg="white", bg="black", font="TkDefaultFont 10")
text.pack(fill="both", expand=True)

count = 0


def colorize(*args):
    global count
    row1, col1 = args[0].startz
    row1, col1 = str(row1), str(col1)
    row2, col2 = args[0].end
    row2, col2 = str(row2), str(col2)
    start = ".".join((row1, col1))
    end = ".".join((row2, col2))
    text.tag_add(str(count), start, end)
    try:
        text.tag_config(str(count), foreground=args[1], font=args[2])
    except IndexError:
        text.tag_config(str(count), foreground=args[1])
    count += 1


def search(event):
    try:
        for i in tokenize.tokenize(io.BytesIO(text.get("1.0", "end").encode("utf-8")).readline):
            if i.type == 1:
                if i.string in keyword.kwlist:
                    colorize(i, "orange")
                elif i.string in dir(builtins):
                    colorize(i, "blue")
                else:
                    colorize(i, "white")
            elif i.type == 2:
                colorize(i, "cyan")
            elif i.type == 3:
                colorize(i, "purple")
            elif i.type == 53:
                if i.string == "," or i.string == "." or i.string == ":":
                    colorize(i, "orange")
                elif i.string == "(" or i.string == ")" or i.string == "[" \
                        or i.string == "]" or i.string == "{" or i.string == "}":
                    colorize(i, "darkred")
                else:
                    colorize(i, "green")
            elif i.type == 57:
                colorize(i, "grey", "TkDefaultFont 10 italic")
    except tokenize.TokenError:
        pass


text.bind("<KeyRelease>", search)
root.mainloop()