Je crée un simple éditeur de texte dans PyQt4
par Pygments
Syntex surligneur. J'ai le code suivant.« RuntimeError: profondeur de récursivité maximale dépassée tout en appelant un objet Python » erreur en python 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_())
Lorsque la première fois, il la charge correctement en sortie, mais si je tape un seul mot en QTextEdit
il attend pendant 1-2 secondes et affiche l'erreur suivante:
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
Est-ce que beaucoup de choses peuvent ne m'aide pas. Je ne sais pas où je me trompe. Aidez-moi, s'il vous plaît.
Merci beaucoup pour cela, il a aidé. Et savez-vous comment conserver la position du curseur après 'setText'? –
Vous devez obtenir la position du texte avant la modification et la restaurer après. Edited ma réponse pour montrer comment cela pourrait être fait. – mata
Merci beaucoup. C'est parfait. Vraiment apprécier votre aide! ** EDIT **: Pourquoi entrer la clé ne fonctionne pas ici? –