问题
I have troubles with saving note(text from QPlainTextEdit). I need only saving in txt format. After typing text and clicking button program displays error 'expected string or bytes-like object not nonetype'.Notepad's program starts from class fileeki till class fileush. I use Python 3.7, PyQt5 and QtDesigner for creating interface.Opening works well, but not saving.Please download all elements of my project. Also there is modules, which you must install.Thanks for trying.
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 to my project on github: https://github.com/iMAGA07/notepadd
回答1:
The error is because you are not actually using the returned data from the file dialog:
def saveac(self):
path = QFileDialog.getSaveFileName(self, "Save file", "", "Text files (*.txt)")
if not path:
return
self._save_to_path(self.path) # <-- here!
Also, the getSaveFileName static returns a tuple composed of file path and selected filter strings, and both of them could be empty if the dialog is cancelled, so if not path
would always fail.
Check the returned data and call the _save_to_path accordingly:
def saveac(self):
path, filter = QFileDialog.getSaveFileName(self, "Save file", "", "Text files (*.txt)")
if not path:
return
self._save_to_path(path)
来源:https://stackoverflow.com/questions/59135406/saving-text-from-qplaintextedit-by-qfiledialog-and-creating-txt-file