返回顶部
首页 > 资讯 > 后端开发 > Python >Python3中PyQt5简单实现文件打开及保存
  • 284
分享到

Python3中PyQt5简单实现文件打开及保存

2024-04-02 19:04:59 284人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

本文主要介绍了python3中PyQt5简单实现文件打开及保存,分享给大家,具体如下: # -*- coding: utf-8 -*- # FORM implementati

本文主要介绍了python3中PyQt5简单实现文件打开及保存,分享给大家,具体如下:


# -*- coding: utf-8 -*-
 
# FORM implementation generated from reading ui file 'filemanage.ui'
#
# Created by: PyQt5 UI code generator 5.12.2
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QFileInfo
from PyQt5.QtWidgets import QFileDialog
 
 
class Ui_Form(object):
    save_path = ''
 
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(484, 412)
        self.open_path_text = QtWidgets.QLineEdit(Form)
        self.open_path_text.setGeometry(QtCore.QRect(40, 20, 331, 20))
        self.open_path_text.setObjectName("open_path_text")
        self.open_path_but = QtWidgets.QPushButton(Form)
        self.open_path_but.setGeometry(QtCore.QRect(380, 20, 75, 23))
        self.open_path_but.setObjectName("open_path_but")
        self.save_path_but = QtWidgets.QPushButton(Form)
        self.save_path_but.setGeometry(QtCore.QRect(380, 50, 75, 23))
        self.save_path_but.setObjectName("save_path_but")
        self.save_path_text = QtWidgets.QLineEdit(Form)
        self.save_path_text.setGeometry(QtCore.QRect(40, 50, 331, 20))
        self.save_path_text.setObjectName("save_path_text")
        self.text_value = QtWidgets.QTextEdit(Form)
        self.text_value.setGeometry(QtCore.QRect(10, 90, 461, 281))
        self.text_value.setObjectName("text_value")
        self.save_but = QtWidgets.QPushButton(Form)
        self.save_but.setGeometry(QtCore.QRect(190, 380, 75, 23))
        self.save_but.setObjectName("save_but")
 
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
 
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.open_path_text.setPlaceholderText(_translate("Form", "打开地址"))
        self.open_path_but.setText(_translate("Form", "浏览"))
        self.save_path_but.setText(_translate("Form", "浏览"))
        self.save_path_text.setPlaceholderText(_translate("Form", "保存地址"))
        self.save_but.setText(_translate("Form", "保存"))
        self.open_path_but.clicked.connect(self.open_event)
        self.save_path_but.clicked.connect(self.save_event)
        self.save_but.clicked.connect(self.save_text)
 
    def open_event(self):
        _translate = QtCore.QCoreApplication.translate
        directory1 = QFileDialog.getOpenFileName(None, "选择文件", "H:/")
        print(directory1)  # 打印文件夹路径
        path = directory1[0]
        self.open_path_text.setText(_translate("Form", path))
        if path is not None:
            with open(file=path, mode='r+', encoding='utf-8') as file:
                self.text_value.setPlainText(file.read())
 
    def save_event(self):
        global save_path
        _translate = QtCore.QCoreApplication.translate
        fileName2, ok2 = QFileDialog.getSaveFileName(None, "文件保存", "H:/")
        print(fileName2)  # 打印保存文件的全部路径(包括文件名和后缀名)
        save_path = fileName2
        self.save_path_text.setText(_translate("Form", save_path))
 
    def save_text(self):
        global save_path
        if save_path is not None:
            with open(file=save_path, mode='a+', encoding='utf-8') as file:
                file.write(self.text_value.toPlainText())
            print('已保存!')
            self.text_value.clear()
 
            '''
            directory1 = QFileDialog.getExistingDirectory(self, "选择文件夹", "/")
            print(directory1)  # 打印文件夹路径
            text.setText(_translate("Form", directory1))
            fileName, filetype = QFileDialog.getOpenFileName(self, "选择文件", "/", "All Files (*);;Text Files (*.txt)")
            print(fileName, filetype)  # 打印文件全部路径(包括文件名和后缀名)和文件类型
            print(fileName)  # 打印文件全部路径(包括文件名和后缀名)
            text.setText(_translate("Form", fileName))
            fileinfo = QFileInfo(fileName)
            print(fileinfo)  # 打印与系统相关的文件信息,包括文件的名字和在文件系统中位置,文件的访问权限,是否是目录或符合链接,等等。
            file_name = fileinfo.fileName()
            print(file_name)  # 打印文件名和后缀名
            file_suffix = fileinfo.suffix()
            print(file_suffix)  # 打印文件后缀名
            file_path = fileinfo.absolutePath()
            print(file_path)  # 打印文件绝对路径(不包括文件名和后缀名)
            files, ok1 = QFileDialog.getOpenFileNames(self, "多文件选择", "/", "所有文件 (*);;文本文件 (*.txt)")
            print(files, ok1)  # 打印所选文件全部路径(包括文件名和后缀名)和文件类型
            fileName2, ok2 = QFileDialog.getSaveFileName(self, "文件保存", "/", "图片文件 (*.png);;(*.jpeg)")
            print(fileName2)  # 打印保存文件的全部路径(包括文件名和后缀名)
            '''
 
if __name__ == "__main__":
    import sys
 
    app = QtWidgets.QApplication(sys.argv)
    widget = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(widget)
    widget.show()
    sys.exit(app.exec_())

到此这篇关于python3中PyQt5简单实现文件打开及保存的文章就介绍到这了,更多相关PyQt5 文件打开保存内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python3中PyQt5简单实现文件打开及保存

本文链接: https://lsjlt.com/news/127987.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作