PyQt4 python中的“RuntimeError:调用Python对象时超出最大递归深度”错误

Posted

技术标签:

【中文标题】PyQt4 python中的“RuntimeError:调用Python对象时超出最大递归深度”错误【英文标题】:"RuntimeError: maximum recursion depth exceeded while calling a Python object" error in PyQt4 python 【发布时间】:2017-03-14 06:44:13 【问题描述】:

我正在通过Pygments 合成荧光笔在PyQt4 中创建一个简单的文本编辑器。我有以下代码。

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 

做了很多事都无法自拔。我不知道我在哪里做错了。请帮帮我。

【问题讨论】:

【参考方案1】:

您的highlighter() 函数调用area.setText(),它将发出连接到highlightertextChanged 信号,highlighter 调用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)

【讨论】:

非常感谢你,它帮助了。你知道如何在setText之后保留光标位置吗? 您需要在编辑之前获取文本位置并在之后恢复它。编辑了我的答案以显示如何做到这一点。 非常感谢。那很完美。非常感谢您的帮助! 编辑:为什么输入键在这里不起作用? 您使用带有stripall=True 的词法分析器,从格式化文本的末尾删除空格

以上是关于PyQt4 python中的“RuntimeError:调用Python对象时超出最大递归深度”错误的主要内容,如果未能解决你的问题,请参考以下文章

在pyqt4 python中的QTableWidget中存储列表值

QListView 不接受使用 Python PyQt4 在 QMainWindow 中的丢弃

如何以编程方式更改/更新 Python PyQt4 TableView 中的数据?

python3.6.8 安装pyqt4失败如何解决?

QTableWidget 和 PyQt4 中的列宽

运动生成器 2016 PyQt4