pyqt5写一个文本处理软件

发布时间:2024年01月16日

参考资料https://www.zhihu.com/column/pyqt5

导入安装包

pip install PyQt5 -i https://pypi.douban.com/simple
pip install PyQt5-tools -i https://pypi.douban.com/simple

pyqt5 designer

如何将生成的UI文件转化为py代码

pyuic5 main.ui -o main.py

好的,下面是一个简单的文件处理程序页面的设计代码示例:

文件处理示例

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QFileDialog


class FileProcessor(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        # 设置窗口标题
        self.setWindowTitle('文件处理程序')


        # 创建主窗口部件
        main_widget = QWidget()
        self.setCentralWidget(main_widget)


        # 创建垂直布局
        vbox = QVBoxLayout()


        # 创建水平布局
        hbox1 = QHBoxLayout()
        hbox2 = QHBoxLayout()


        # 添加标签和文本框到第一行水平布局
        label1 = QLabel('输入文件路径:')
        self.input_path = QLineEdit()
        hbox1.addWidget(label1)
        hbox1.addWidget(self.input_path)


        # 添加打开文件按钮到第一行水平布局
        open_button = QPushButton('打开文件')
        open_button.clicked.connect(self.open_file_dialog)
        hbox1.addWidget(open_button)


        # 添加标签和文本框到第二行水平布局
        label2 = QLabel('输出文件路径:')
        self.output_path = QLineEdit()
        hbox2.addWidget(label2)
        hbox2.addWidget(self.output_path)


        # 添加保存文件按钮到第二行水平布局
        save_button = QPushButton('保存文件')
        save_button.clicked.connect(self.save_file_dialog)
        hbox2.addWidget(save_button)


        # 添加两个水平布局到垂直布局
        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)


        # 添加文本框到垂直布局
        self.text_edit = QTextEdit()
        vbox.addWidget(self.text_edit)


        # 设置主窗口部件的布局
        main_widget.setLayout(vbox)


    def open_file_dialog(self):
        # 打开文件对话框
        file_path, _ = QFileDialog.getOpenFileName(self, '打开文件', '', 'Text Files (*.txt);;All Files (*)')
        if file_path:
            self.input_path.setText(file_path)


    def save_file_dialog(self):
        # 保存文件对话框
        file_path, _ = QFileDialog.getSaveFileName(self, '保存文件', '', 'Text Files (*.txt);;All Files (*)')
        if file_path:
            self.output_path.setText(file_path)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    fp = FileProcessor()
    fp.show()
    sys.exit(app.exec_())

文件界面

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(769, 311)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setObjectName("label_2")
        self.verticalLayout.addWidget(self.label_2)
        self.listWidget = QtWidgets.QListWidget(Dialog)
        self.listWidget.setObjectName("listWidget")
        self.verticalLayout.addWidget(self.listWidget)
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setObjectName("label_3")
        self.verticalLayout.addWidget(self.label_3)
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setObjectName("pushButton_2")
        self.verticalLayout.addWidget(self.pushButton_2)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.listView = QtWidgets.QListView(Dialog)
        self.listView.setObjectName("listView")
        self.verticalLayout.addWidget(self.listView)
        self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.verticalLayout.addWidget(self.buttonBox)

        self.retranslateUi(Dialog)
        self.buttonBox.accepted.connect(Dialog.accept) # type: ignore
        self.buttonBox.rejected.connect(Dialog.reject) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label_2.setText(_translate("Dialog", "输入文件"))
        self.label_3.setText(_translate("Dialog", "操作"))
        self.pushButton_2.setText(_translate("Dialog", "双周数据图表"))
        self.pushButton.setText(_translate("Dialog", "周销明细"))
        self.label.setText(_translate("Dialog", "输出文件"))


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QDialog()
    ui = Ui_Dialog()

    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
文章来源:https://blog.csdn.net/ZXG20000/article/details/135631714
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。