Python 3.x 如何打开可读文件并跳转到PyQt5中的特定行?
我已经在PyQt5中使用QLabel创建了一个链接。现在我想,当我点击那个链接时,它应该会打开文件并自动将光标移到第5行。使用PyQt5或至少在Python3中使用PyQt5实现此目的的方法或代码是什么。您可以在QPlainTextEdit中显示文本,并使用QTextCursor移动到第5行Python 3.x 如何打开可读文件并跳转到PyQt5中的特定行?,python-3.x,pyqt5,Python 3.x,Pyqt5,我已经在PyQt5中使用QLabel创建了一个链接。现在我想,当我点击那个链接时,它应该会打开文件并自动将光标移到第5行。使用PyQt5或至少在Python3中使用PyQt5实现此目的的方法或代码是什么。您可以在QPlainTextEdit中显示文本,并使用QTextCursor移动到第5行 import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * class Te
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class Template(QWidget):
def __init__(self):
super().__init__()
with open ('file.txt') as file:
text = file.read()
editor = QPlainTextEdit()
editor.setPlainText(text)
cursor = editor.textCursor()
cursor.movePosition(QTextCursor.NextBlock, QTextCursor.MoveAnchor, 4)
editor.setTextCursor(cursor)
vbox = QVBoxLayout(self)
vbox.addWidget(editor)
if __name__ == '__main__':
app = QApplication(sys.argv)
gui = Template()
gui.show()
sys.exit(app.exec_())
什么样的文件?您是否在GUI中显示该文件?它将是纯文本文件。在GUI中显示将是一个编辑器,可以是记事本。