通过 QFileDialog 从 QPlainTextEdit 保存文本并创建 .txt 文件

Saving text from QPlainTextEdit by QFileDialog and creating .txt file

我在保存笔记(来自 QPlainTextEdit 的文本)时遇到了问题。我只需要保存为txt格式。键入文本并单击按钮后,程序显示错误 'expected string or bytes-like object not nonetype'。记事本程序从 class fileeki 开始,直到 class fileush。我使用 Python 3.7、PyQt5 和 QtDesigner 创建 interface.Opening 效果很好,但 saving.Please 下载我项目的所有元素。还有模块,您必须 install.Thanks 才能尝试。

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPlainTextEdit
from PyQt5.QtWidgets import QLabel, QPushButton, QMessageBox, QFileDialog
from PyQt5.QtGui import QPixmap


class fileeki(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__()
        uic.loadUi('uineweki.ui', self)

        self.path = None

        self.pushButton.clicked.connect(self.opening_run)
        self.pushButton_2.clicked.connect(self.saving_run)
        self.pushButton_3.clicked.connect(self.saveac)
        self.pushButton_5.clicked.connect(self.new_run)

    def dialog_critical(self, s):
        dlg = QMessageBox(self)
        dlg.setText(s)
        dlg.setIcon(QMessageBox.Critical)
        dlg.show()

    def opening_run(self):
        path, _ = QFileDialog.getOpenFileName(self, "Open file", "", "Text files (*.txt)")

        if path:
            try:
                with open(path, 'rU') as f:
                    text = f.read()

            except Exception as e:
                self.dialog_critical(str(e))

            else:
                self.path = path
                self.plainTextEdit.setPlainText(text)

    def saving_run(self):
        if self.path is None:

            return self.saveac()

        self._save_to_path(self.path)

    def saveac(self):
        path = QFileDialog.getSaveFileName(self, "Save file", "", "Text files (*.txt)")

        if not path:

            return

        self._save_to_path(self.path)

    def _save_to_path(self, path):
        text = self.plainTextEdit.toPlainText()
        try:
            with open(path, 'w') as f:
                f.write(text)

        except Exception as e:
            self.dialog_critical(str(e))

        else:
            self.path = path

    def new_run(self):
        self.plainTextEdit.clear()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = fileeki()
    ex.show()
    sys.exit(app.exec())

Link 到我在 github 上的项目:https://github.com/iMAGA07/notepadd

错误是因为您实际上没有使用文件对话框返回的数据:

    def saveac(self):
        path = QFileDialog.getSaveFileName(self, "Save file", "", "Text files (*.txt)")

        if not path:

            return

        self._save_to_path(self.path) # <-- here!

此外,getSaveFileName static returns 一个由 文件路径 selected filter 字符串组成的元组,并且它们都是如果对话框被取消,则可能为空,因此 if not path 总是会失败。

检查返回的数据并相应地调用_save_to_path:

    def saveac(self):
        path, filter = QFileDialog.getSaveFileName(self, "Save file", "", "Text files (*.txt)")

        if not path:

            return

        self._save_to_path(path)