2017-03-14 75 views
1

我在PyQt4Pygments syntex熒光筆中創建了一個簡單的文本編輯器。我有以下代碼。「RuntimeError:調用Python對象時超出最大遞歸深度」PyQt4中的錯誤python

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信號,其中要求area.setText()這...等等。

對於QTextEdittextChaged信號總是發出時setText()方法被調用,即使可見文本內容實際上並沒有改變,因爲它的內部表示確實發生了改變。

你的情況簡單的解決方法是,當你調用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) 
+0

謝謝你很多的是,它幫助。你知道如何在'setText'之後保留光標位置嗎? –

+0

您需要在編輯之前獲取文本位置,然後將其恢復。編輯我的答案,以顯示如何做到這一點。 – mata

+0

非常感謝。那很完美。非常感謝您的幫助! **編輯**:爲什麼輸入密鑰在這裏不工作? –

相關問題