Python Pygments 颜色错误
Posted
技术标签:
【中文标题】Python Pygments 颜色错误【英文标题】:Python Pygments color error 【发布时间】:2015-08-16 07:26:33 【问题描述】:我一直在尝试为 python 制作一个带有语法高亮的简单文本板。但问题是当我打开引用一个字符串广告 pt 结束引号时,文本是引号不会变回,这是它的外观:
这是我当前的代码:
from tkinter import * # All from Tkinter
# =============================================================================
class vwin():
def __init__(self,args,master=None):
if type(args[3]) == type(self):
self.window = Toplevel(args[3].window)
elif args[3].lower() == 'tk':
self.window = Tk()
self.window.title(args[0])
self.window.geometry(args[1])
self.window.state(args[2])
def mainloop():
self.window.mainloop()
class customtext:
def __init__(self,master):
import tkinter.scrolledtext as scrolltxt
self.textarea = scrolltxt.ScrolledText(master)
self.textarea.configure(bg = "#555555",
insertbackground = "#dddddd",
font = "Courier 14 bold",
tabs = 40,
foreground="#dddddd")
self.textarea.pack(fill = BOTH, expand = True)
# TAGS ----------------------------------------------------------------
self.textarea.tag_configure("Token.Comment",
foreground="#00ff00")
self.textarea.tag_configure("Token.Literal.String",
foreground="#ff99cc")
self.textarea.tag_configure("Token.Keyword",
foreground="#aaaa00")
self.textarea.tag_configure("Token.Name.Function",
foreground="#ffff00")
def highlight(self,event=None):
# Imports PythonLex from Pygments -----
from pygments import lex
from pygments.lexers import PythonLexer
# Content Search ------------------------------------------------------
self.textarea.mark_set("range_start", "1.0")
data = self.textarea.get("1.0", "end-1c")
for token, content in lex(data, PythonLexer()):
self.textarea.mark_set("range_end",
"range_start + %dc" % len(content))
self.textarea.tag_add(str(token), "range_start", "range_end")
self.textarea.mark_set("range_start", "range_end")
def stat_title(self,event=None):
ind = self.textarea.index(INSERT)
root.window.title('VenomPad Mini' + "[Index:" + str(ind) + "]")
def select_all(self,event):
self.textarea.tag_add(SEL, "1.0", END)
self.textarea.mark_set(INSERT, "1.0")
self.textarea.see(INSERT)
# =============================================================================
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
root = vwin(['VenomPad Mini','640x480-25-25','normal','Tk'])
text = customtext(root.window)
root.window.bind("<Key>", text.highlight)
root.window.bind("<KeyRelease>", text.stat_title)
root.window.bind("<Control-Key-a>", text.select_all)
root.window.mainloop()
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
【问题讨论】:
【参考方案1】:在另一个范围内标记一个范围,不会缩短范围。
我能想到的最简单的事情是在添加标签之前删除现有标签:
def highlight(self,event=None):
...
data = self.textarea.get("1.0", "end-1c")
# Remove the existing tags from textarea
for tag in self.textarea.tag_names():
self.textarea.tag_remove(tag, "1.0", "end")
for token, content in lex(data, PythonLexer()):
self.textarea.mark_set("range_end",
"range_start + %dc" % len(content))
self.textarea.tag_add(str(token), "range_start", "range_end")
self.textarea.mark_set("range_start", "range_end")
...
【讨论】:
以上是关于Python Pygments 颜色错误的主要内容,如果未能解决你的问题,请参考以下文章
Pygments 代码中的行号在 Windows 上的 xampp 中突出显示
cx_Freeze:Python 错误主脚本。找不到模块错误:没有名为 pygments.lexers.python 的模块