主頁(yè) > 知識(shí)庫(kù) > Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存

Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存

熱門標(biāo)簽:地圖標(biāo)注自己和別人標(biāo)注區(qū)別 ai電銷機(jī)器人的優(yōu)勢(shì) 聊城語(yǔ)音外呼系統(tǒng) 騰訊地圖標(biāo)注沒法顯示 南陽(yáng)打電話機(jī)器人 孝感營(yíng)銷電話機(jī)器人效果怎么樣 商家地圖標(biāo)注海報(bào) 海外網(wǎng)吧地圖標(biāo)注注冊(cè) 打電話機(jī)器人營(yíng)銷

本文主要介紹了Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存,分享給大家,具體如下:

# -*- 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)  # 打印與系統(tǒng)相關(guān)的文件信息,包括文件的名字和在文件系統(tǒng)中位置,文件的訪問(wèn)權(quán)限,是否是目錄或符合鏈接,等等。
            file_name = fileinfo.fileName()
            print(file_name)  # 打印文件名和后綴名
            file_suffix = fileinfo.suffix()
            print(file_suffix)  # 打印文件后綴名
            file_path = fileinfo.absolutePath()
            print(file_path)  # 打印文件絕對(duì)路徑(不包括文件名和后綴名)
            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_())

到此這篇關(guān)于Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存的文章就介紹到這了,更多相關(guān)PyQt5 文件打開保存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • pyqt5 實(shí)現(xiàn) 下拉菜單 + 打開文件的示例代碼
  • PyQt5打開文件對(duì)話框QFileDialog實(shí)例代碼

標(biāo)簽:撫州 迪慶 六盤水 聊城 南寧 牡丹江 楊凌 揚(yáng)州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存》,本文關(guān)鍵詞  Python3,中,PyQt5,簡(jiǎn)單,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開及保存的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章