python tkinter文本小部件中的Pygments语法荧光笔 [英] Pygments syntax highlighter in python tkinter text widget

查看:65
本文介绍了python tkinter文本小部件中的Pygments语法荧光笔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将语法高亮与 tkinter 文本小部件结合起来.但是,使用在 this 上找到的代码发布,我无法让它工作.没有错误,但文本没有突出显示,并且在每个字符后跳过一行.如果有更好的方法将语法突出显示与 tkinter 文本小部件结合在一起,我会很高兴听到它.这是我能找到的最小的代码来复制这个问题:

I have been trying to incorporate syntax highlighting with the tkinter text widget. However, using the code found on this post, I cannot get it to work. There are no errors, but the text is not highlighted and a line is skipped after each character. If there is a better way to incorporate syntax highlighting with the tkinter text widget, I would be happy to hear it. Here is the smallest code I could find that replicates the issue:

import Tkinter
import ScrolledText
from pygments import lex
from pygments.lexers import PythonLexer

root = Tkinter.Tk(className=" How do I put an end to this behavior?")
textPad = ScrolledText.ScrolledText(root, width=100, height=80)
textPad.tag_configure("Token.Comment", foreground="#b21111")
code = textPad.get("1.0", "end-1c")
# Parse the code and insert into the widget
def syn(event=None):
    for token, content in lex(code, PythonLexer()):
        textPad.insert("end", content, str(token))
textPad.pack()
root.bind("<Key>", syn)
root.mainloop()

到目前为止,我还没有找到解决这个问题的方法(否则我不会在这里发帖).任何有关语法高亮 tkinter 文本小部件的帮助将不胜感激.

So far, I have not found a solution to this problem (otherwise I would not be posting here). Any help regarding syntax highlighting a tkinter text widget would be appreciated.

注意:这是在带有 Windows 7 的 python 2.7 上.

Note: This is on python 2.7 with Windows 7.

推荐答案

您链接到的问题中的代码更多地是为了突出显示现有文本,而看起来您在键入时试图突出显示它.

The code in the question you linked to was designed more for highlighting already existing text, whereas it looks like you're trying to highlight it as you type.

我可以提供一些建议来帮助您入门,尽管我从未这样做过,也不知道最有效的解决方案是什么.此答案中的解决方案只是一个起点,不能保证它实际上适合您的问题.

I can give some suggestions to get you started, though I've never done this and don't know what the most efficient solution is. The solution in this answer is only a starting point, there's no guarantee it is actually suited to your problem.

简短的概要是:不要设置插入任何内容的绑定.相反,只需突出显示默认绑定插入的内容.

The short synopis is this: don't set up a binding that inserts anything. Instead, just highlight what was inserted by the default bindings.

要做到这一点,第一步是绑定而不是.不同之处在于 将在插入字符之后 发生,而 发生在 之前> 插入了一个字符.

To do this, the first step is to bind on <KeyRelease> rather than <Key>. The difference is that <KeyRelease> will happen after a character has been inserted whereas <Key> happens before a character is inserted.

其次,您需要从词法分析器中获取标记,并将标记应用于每个标记的文本.为此,您需要跟踪词法分析器在文档中的位置,然后使用标记的长度来确定标记的结尾.

Second, you need to get tokens from the lexer, and apply tags to the text for each token. To do that you need to keep track where in the document the lexer is, and then use the length of the token to determine the end of the token.

在以下解决方案中,我创建了一个标记 ("range_start") 来指定文件中 pygments 词法分析器所在的当前位置,然后计算标记 "range_end" 基于开始,以及 pygments 返回的令牌的长度.我不知道这在多字节字符面前有多健壮.现在,让我们假设单字节字符.

In the following solution I create a mark ("range_start") to designate the current location in the file where the pygments lexer is, and then compute the mark "range_end" based on the start, and the length of the token returned by pygments. I don't know how robust this is in the face of multi-byte characters. For now, lets assume single byte characters.

def syn(event=None):
    textPad.mark_set("range_start", "1.0")
    data = textPad.get("1.0", "end-1c")
    for token, content in lex(data, PythonLexer()):
        textPad.mark_set("range_end", "range_start + %dc" % len(content))
        textPad.tag_add(str(token), "range_start", "range_end")
        textPad.mark_set("range_start", "range_end")

这是非常低效的,因为它会在每次按键时将突出显示重新应用于整个文档.有一些方法可以将其最小化,例如仅在每个单词之后突出显示,或者在 GUI 空闲时,或某种其他类型的触发器.

This is crazy inefficient since it re-applies the highlighting to the whole document on every keypress. There are ways to minimize that, such as only highlighting after each word, or when the GUI goes idle, or some other sort of trigger.

这篇关于python tkinter文本小部件中的Pygments语法荧光笔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆