方法1:
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog, QTextEdit
from PySide6.QtGui import QAction
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('打开txt文件')
self.setGeometry(100, 100, 500, 500)
self.text_area = QTextEdit(self)
self.setCentralWidget(self.text_area)
open_action = QAction('打开', self)
open_action.triggered.connect(self.file_open)
file_menu = self.menuBar().addMenu('文件')
file_menu.addAction(open_action)
def file_open(self):
file_name, _ = QFileDialog.getOpenFileName(self, '打开文件', '', 'Text files (*.txt)')
if file_name:
with open(file_name, 'r') as file:
self.text_area.setPlainText(file.read())
file.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
方法2:
openfile.py
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog, QTextEdit
from PySide6.QtGui import QAction
from openfile_ui import Ui_Form
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.ui.pushButton.released.connect(self.file_open)
def file_open(self):
file_name, _ = QFileDialog.getOpenFileName(self, '打开文件', '', 'Text files (*.txt)')
if file_name:
with open(file_name, 'r', encoding='utf-8') as file:
self.ui.textEdit.setPlainText(file.read())
file.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
openfile_ui.py
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'openfile_ui.ui'
##
## Created by: Qt User Interface Compiler version 6.4.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QPushButton, QSizePolicy, QTextEdit,
QVBoxLayout, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(404, 271)
self.verticalLayoutWidget = QWidget(Form)
self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
self.verticalLayoutWidget.setGeometry(QRect(0, 0, 401, 271))
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.textEdit = QTextEdit(self.verticalLayoutWidget)
self.textEdit.setObjectName(u"textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.pushButton = QPushButton(self.verticalLayoutWidget)
self.pushButton.setObjectName(u"pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"&Open", None))
# retranslateUi
运行结果: