gpt4 book ai didi

Python Pygments 颜色错误

转载 作者:行者123 更新时间:2023-11-28 16:30:53 25 4
gpt4 key购买 nike

我一直在尝试为 python 制作一个带有语法高亮显示的简单文本板。但问题是当我打开引号字符串广告 pt 结束引号时,文本是引号不会变回来,这是它的样子:

Heading

这是我当前的代码:

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()
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

最佳答案

在另一个范围内标记一个范围,不会缩短范围。

我能想到的最简单的事情是在添加标签之前删除现有标签:

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 颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32032815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com