"RuntimeError: maximum recursion depth exceeded while calling a Python object" error in PyQt4 python
"RuntimeError: maximum recursion depth exceeded while calling a Python object" error in PyQt4 python
我正在 PyQt4
通过 Pygments
syntex highlighter 创建一个简单的文本编辑器。我有以下代码。
from PyQt4 import QtCore, QtGui
import time,sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from pygments import highlight
from pygments.lexers import PythonLexer,get_lexer_by_name
from pygments.formatters import HtmlFormatter
def highlighter():
text = area.toPlainText()
result = highlight(text, lexer, formatter)
area.setText(result)
code = 'print ("Hello World")\n# Test Program'
lexer = get_lexer_by_name("python3", stripall=True)
formatter = HtmlFormatter(linenos=False,style='colorful')
formatter.noclasses = True
result = highlight(code, lexer, formatter)
app = QApplication(sys.argv)
w=QWidget()
w.setGeometry(500,400,350,350)
area = QTextEdit(w)
area.setGeometry(0,10,350,340)
area.setText(result)
area.textChanged.connect(highlighter)
w.show()
sys.exit(app.exec_())
第一次加载时输出正确,但如果我在 QTextEdit
中键入一个单词,它会等待 1-2 秒并显示以下错误:
Traceback (most recent call last): File "C:\Users\Home\Desktop\code_highlighter - Copy.py", line 10, in highlight er
result = highlight(text, lexer, formatter) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 85, in highlig ht
return format(lex(code, lexer), formatter, outfile) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 64, in format
formatter.format(tokens, realoutfile) File "C:\Python34\lib\site-packages\pygments\formatter.py", line 95, in format
return self.format_unencoded(tokensource, outfile) File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 850, in format_unencoded
for t, piece in source: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 690, in _wrap_div
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 708, in _wrap_pre
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 727, in _format_lines
for ttype, value in tokensource: File "C:\Python34\lib\site-packages\pygments\lexer.py", line 191, in streamer
for _, t, v in self.get_tokens_unprocessed(text): File "C:\Python34\lib\site-packages\pygments\lexer.py", line 624, in get_token s_unprocessed
statestack = list(stack) RuntimeError: maximum recursion depth exceeded while calling a Python object Traceback (most recent call last): File "C:\Users\Home\Desktop\code_highlighter - Copy.py", line 10, in highlight er
result = highlight(text, lexer, formatter) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 85, in highlig ht
return format(lex(code, lexer), formatter, outfile) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 64, in format
formatter.format(tokens, realoutfile) File "C:\Python34\lib\site-packages\pygments\formatter.py", line 95, in format
return self.format_unencoded(tokensource, outfile) File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 850, in format_unencoded
for t, piece in source: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 690, in _wrap_div
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 708, in _wrap_pre
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 727, in _format_lines
for ttype, value in tokensource: File "C:\Python34\lib\site-packages\pygments\lexer.py", line 191, in streamer
for _, t, v in self.get_tokens_unprocessed(text): File "C:\Python34\lib\site-packages\pygments\lexer.py", line 624, in get_token s_unprocessed
statestack = list(stack) RuntimeError: maximum recursion depth exceeded while calling a Python object
很多事情都是身不由己。我不知道我哪里做错了。请帮助我。
您的 highlighter()
函数调用 area.setText()
,它将发出连接到 highlighter
的 textChanged
信号,后者调用 area.setText()
...等等。
对于 QTextEdit
,当调用 setText()
方法时,总是会发出 textChaged
信号,即使可见文本内容实际上没有改变,因为它的内部表示确实改变。
针对您的情况,一个简单的解决方法是在您调用 setText()
:
时阻止信号传递
def highlighter():
text = area.toPlainText()
result = highlight(text, lexer, formatter)
area.blockSignals(True)
pos = area.textCursor().position()
area.setText(result)
cursor = area.textCursor()
cursor.setPosition(min(pos, len(area.toPlainText())))
area.setTextCursor(cursor)
area.blockSignals(False)
我正在 PyQt4
通过 Pygments
syntex highlighter 创建一个简单的文本编辑器。我有以下代码。
from PyQt4 import QtCore, QtGui
import time,sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from pygments import highlight
from pygments.lexers import PythonLexer,get_lexer_by_name
from pygments.formatters import HtmlFormatter
def highlighter():
text = area.toPlainText()
result = highlight(text, lexer, formatter)
area.setText(result)
code = 'print ("Hello World")\n# Test Program'
lexer = get_lexer_by_name("python3", stripall=True)
formatter = HtmlFormatter(linenos=False,style='colorful')
formatter.noclasses = True
result = highlight(code, lexer, formatter)
app = QApplication(sys.argv)
w=QWidget()
w.setGeometry(500,400,350,350)
area = QTextEdit(w)
area.setGeometry(0,10,350,340)
area.setText(result)
area.textChanged.connect(highlighter)
w.show()
sys.exit(app.exec_())
第一次加载时输出正确,但如果我在 QTextEdit
中键入一个单词,它会等待 1-2 秒并显示以下错误:
Traceback (most recent call last): File "C:\Users\Home\Desktop\code_highlighter - Copy.py", line 10, in highlight er
result = highlight(text, lexer, formatter) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 85, in highlig ht
return format(lex(code, lexer), formatter, outfile) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 64, in format
formatter.format(tokens, realoutfile) File "C:\Python34\lib\site-packages\pygments\formatter.py", line 95, in format
return self.format_unencoded(tokensource, outfile) File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 850, in format_unencoded
for t, piece in source: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 690, in _wrap_div
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 708, in _wrap_pre
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 727, in _format_lines
for ttype, value in tokensource: File "C:\Python34\lib\site-packages\pygments\lexer.py", line 191, in streamer
for _, t, v in self.get_tokens_unprocessed(text): File "C:\Python34\lib\site-packages\pygments\lexer.py", line 624, in get_token s_unprocessed
statestack = list(stack) RuntimeError: maximum recursion depth exceeded while calling a Python object Traceback (most recent call last): File "C:\Users\Home\Desktop\code_highlighter - Copy.py", line 10, in highlight er
result = highlight(text, lexer, formatter) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 85, in highlig ht
return format(lex(code, lexer), formatter, outfile) File "C:\Python34\lib\site-packages\pygments\__init__.py", line 64, in format
formatter.format(tokens, realoutfile) File "C:\Python34\lib\site-packages\pygments\formatter.py", line 95, in format
return self.format_unencoded(tokensource, outfile) File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 850, in format_unencoded
for t, piece in source: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 690, in _wrap_div
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 708, in _wrap_pre
for tup in inner: File "C:\Python34\lib\site-packages\pygments\formatters\html.py", line 727, in _format_lines
for ttype, value in tokensource: File "C:\Python34\lib\site-packages\pygments\lexer.py", line 191, in streamer
for _, t, v in self.get_tokens_unprocessed(text): File "C:\Python34\lib\site-packages\pygments\lexer.py", line 624, in get_token s_unprocessed
statestack = list(stack) RuntimeError: maximum recursion depth exceeded while calling a Python object
很多事情都是身不由己。我不知道我哪里做错了。请帮助我。
您的 highlighter()
函数调用 area.setText()
,它将发出连接到 highlighter
的 textChanged
信号,后者调用 area.setText()
...等等。
对于 QTextEdit
,当调用 setText()
方法时,总是会发出 textChaged
信号,即使可见文本内容实际上没有改变,因为它的内部表示确实改变。
针对您的情况,一个简单的解决方法是在您调用 setText()
:
def highlighter():
text = area.toPlainText()
result = highlight(text, lexer, formatter)
area.blockSignals(True)
pos = area.textCursor().position()
area.setText(result)
cursor = area.textCursor()
cursor.setPosition(min(pos, len(area.toPlainText())))
area.setTextCursor(cursor)
area.blockSignals(False)