1 回答
TA贡献1796条经验 获得超4个赞
问题是,如果行号小于文本中的行数,则返回一个有效的行号,并且在开始时 QTextEdit 为空,因此它将失败。一种可能的解决方案是添加结束行“\n”,直到获得行数。findBlockByLineNumber()QTextBlock
import sys
from PyQt5.QtWidgets import (
QMainWindow,
QApplication,
QLineEdit,
QPushButton,
QTextEdit,
QGridLayout,
QWidget,
)
from PyQt5.QtGui import QTextCursor
class VentanaFindText(QMainWindow):
def __init__(self):
super(VentanaFindText, self).__init__()
self.setWindowTitle("find text - QTextEdit")
self.resize(475, 253)
self.line_buscar = QLineEdit()
self.btn_buscar = QPushButton("buscar",)
self.text_edit = QTextEdit()
central_widget = QWidget()
self.setCentralWidget(central_widget)
grid_layout = QGridLayout(central_widget)
grid_layout.addWidget(self.line_buscar, 0, 0)
grid_layout.addWidget(self.btn_buscar, 0, 1)
grid_layout.addWidget(self.text_edit, 1, 0, 1, 2)
self.btn_buscar.clicked.connect(self.gotoLine)
def gotoLine(self):
text = self.line_buscar.text()
try:
n = int(text)
except ValueError:
print("Cannot convert '{}' to integer number".format(text))
else:
if n < 1:
print("The number must be greater than 1")
return
doc = self.text_edit.document()
self.text_edit.setFocus()
if n > doc.blockCount():
self.text_edit.insertPlainText("\n" * (n - doc.blockCount()))
cursor = QTextCursor(doc.findBlockByLineNumber(n - 1))
self.text_edit.setTextCursor(cursor)
if __name__ == "__main__":
app = QApplication(sys.argv)
ventana = VentanaFindText()
ventana.show()
sys.exit(app.exec_())
添加回答
举报