Python3中PyQt5简单实现文件打开及保存
作者:LOONGV 时间:2023-10-17 21:45:52
本文主要介绍了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_())
来源:https://blog.csdn.net/u013032852/article/details/93996214
标签:PyQt5,文件,打开,保存
0
投稿
猜你喜欢
python 图像增强算法实现详解
2023-10-27 10:12:47
SQL Server 远程更新目标表数据的存储过程
2024-01-21 07:38:04
记一次成功的sql注入入侵检测附带sql性能优化
2011-09-30 11:29:39
Nuxt3+ElementPlus构建打包部署全过程
2023-07-02 16:28:51
python使用QQ邮箱实现自动发送邮件
2021-03-03 22:10:06
ACCESS数据库的压缩,备份,还原,下载,删除的实现
2024-01-18 16:16:26
Python配置虚拟环境图文步骤
2023-10-13 01:37:40
Python实现双向链表基本操作
2022-07-22 10:14:12
解决Python3错误:SyntaxError: unexpected EOF while parsin
2022-02-08 10:18:32
关于Vue的URL转跳与参数传递方式
2024-05-09 09:06:02
隐藏修改文件时间和文件属性的ASP脚本
2011-03-17 11:15:00
Python matplotlib实现折线图的绘制
2022-05-03 11:14:58
javascript 获取select下拉列表值的代码
2024-04-19 09:50:04
Mysql实现简易版搜索引擎的示例代码
2024-01-28 10:42:32
Python 写入训练日志文件并控制台输出解析
2021-10-11 01:27:10
一段压缩MS SQLServer日志的语句
2024-01-14 23:42:07
Python实现图片格式转换
2023-08-03 04:58:57
python并发2之使用asyncio处理并发
2022-01-11 23:41:28
Javascript学习笔记之数组的遍历和 length 属性
2024-04-18 10:53:43
Python3安装Pillow与PIL的方法
2022-09-27 10:21:28