经过测试,我得出结论是错误
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
text.mainloop()
File "C:\Program Files\Python32\lib\tkinter\__init__.py", line 1009, \
in mainloop
self.tk.mainloop(n)
RuntimeError: maximum recursion depth exceeded
只有在INSERT位置之前有一个字符且用户键入Backspace后才会出现。这是我的代码:
# Import
import tkinter
import tkinter.scrolledtext
import idlelib.Delegator
import idlelib.WidgetRedirector
import idlelib.Percolator
import idlelib.ColorDelegator
import time
import re
# Class
class Highlighter(idlelib.Delegator.Delegator):
def __init__(self):
idlelib.Delegator.Delegator.__init__(self)
self._syntaxes = {}
self._compiled = ""
def setdelegate(self, delegate):
idlelib.Delegator.Delegator.setdelegate(self, delegate)
if delegate is not None:
self.setup()
def insert(self, index, chars, *args):
index = self.index(index)
self.delegate.insert(index, chars, *args)
self.update(index, index + "+%dc" % len(chars))
def delete(self, index1, index2=None):
index = self.index(index1)
self.delete(index1, index2)
self.update(index)
def update(self, index1, index2=None):
self.tag_add("TODO", index1, index2)
self.after(1, self.syntax_highlight)
def setup(self):
background = self.cget("background")
self.tag_configure("TODO", foreground=background, background=None)
def syntax_configure(self, tag, syntax):
"""Configure syntax TAG with pattern SYNTAX."""
self._syntaxes[tag] = syntax
syntax_config = syntax_configure
def syntax_cget(self, tag):
"""Return the pattern of TAG."""
return self._syntaxes[tag]
def syntax_delete(self, tag):
"""Delete syntax TAG from the highlighter."""
self._syntaxes.pop(tag)
def syntax_clear(self):
"""Clear all syntaxes."""
self._syntaxes.clear()
def syntax_compile(self):
"""Compile all syntaxes."""
return "|".join("(?P<%s>%s)" % (tag, pattern)
for tag, pattern in self._syntaxes.items())
def syntax_highlight(self, start="1.0", end="end"):
start = self.index(start)
end = self.index(end)
pattern = self.syntax_compile()
if pattern:
for match in re.finditer(pattern, self.get(start, end)):
tag, sequence = list(match.groupdict().items())[0]
start, stop = match.span()
self.tag_remove("TODO", "1.0", "1.0+%dc" % match.span()[1])
self.tag_add(tag, "1.0+%dc" % start, "1.0+%dc" % stop)
self.tag_delete("TODO")
text = tkinter.Text()
text.pack()
p = idlelib.Percolator.Percolator(text)
h = Highlighter()
p.insertfilter(h)
text.mainloop()
如果有人能解释递归的原因/来源是什么,那将非常感激。谢谢!
答案 0 :(得分:1)
问题在于,self.delete(index1, index2)
中的delete
应该是self.delegate.delete(index1, index2)
而不是{{1}}。