我正在用PySide编写一个编剧应用程序。我想要的是在用户键入时将字符转换为大写。
下面这段代码在每次我添加一个字符时都会给出一个运行时错误"maximum recursion depth exceeded“。我知道这意味着什么,为什么它会发生,但有没有不同的方式呢?
self.cursor = self.recipient.textCursor()
self.cursor.movePosition(QTextCursor.StartOfLine)
self.cursor.movePosition(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
self.curtext = self.cursor.selectedText()
if len(self.curtext) > len(self.prevText):
self.cursor.insertText(self.curtext.upper())
self.cursor.clearSelection()
self.prevText = self.curtext只要文本编辑小部件中的文本发生更改,就会运行上面的代码。if语句阻止代码在用户未插入文本时运行。
发布于 2015-01-06 20:43:50
你得到一个递归错误,可能是因为当将输入固定为大写时,你改变了你的内容,并再次触发了完全相同的固定例程。此外,您还会不断地更改整行,而只有一部分发生了更改,需要进行修复。
幸运的是,Qt可以使用QTextCharFormat自己完成这项工作。这是一个自动将所有文本保留为QLineEdit大写的示例。你还可以做更多的with it,比如给文本加下划线或加粗……
示例:
from PySide import QtGui
app = QtGui.QApplication([])
widget = QtGui.QTextEdit()
fmt = QtGui.QTextCharFormat()
fmt.setFontCapitalization(QtGui.QFont.AllUppercase)
widget.setCurrentCharFormat(fmt)
widget.show()
app.exec_()https://stackoverflow.com/questions/27755004
复制相似问题