简体   繁体   English

PyQt getsavefilename取消保存时显示错误“ Python已停止工作”

[英]PyQt getsavefilename shows error “Python has stopped working” when canceling save

I have a problem with getsavefilename when saving a file from a textedit. 从textedit保存文件时, getsavefilename出现问题。

When I am saving a file it is working there is no problem but when I want to close the save dialog without saving it is giving me error: 当我保存文件时,它没有问题,但是当我不保存就关闭保存对话框时,出现错误:

python has stopped working python已经停止工作

I'am using python 3.6 我正在使用python 3.6

Code

def fileSave(self):
    filename = QFileDialog.getSaveFileName(self, 'Save A File', '/home')

    with open(filename[0], 'w') as f:
        text = self.textEdit.toPlainText()
        f.write(text)
        f.close()

Error screenshot 错误截图 1

When you cancel the QFileDialog it returns as an empty text, and when you want to open a file with a non-existent name, it generates that error. 当您取消QFileDialog它以空文本形式返回,并且当您要打开一个不存在的名称的文件时,它将生成该错误。 Also if you are using with to open the file it is not necessary to close the file, just quit the with it will be closed automatically 另外,如果您使用with来打开文件,则不必关闭文件,只需退出with它就会自动关闭

def fileSave(self):
    filename, _ = QFileDialog.getSaveFileName(self, 'Save A File', '/home')

    if filename != "":
        with open(filename, 'w') as f:
            text = self.textEdit.toPlainText()
            f.write(text)

Update: 更新:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtPrintSupport import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        lay = QVBoxLayout(self)
        self.textEdit = QTextEdit()
        self.button = QPushButton("Print")
        lay.addWidget(self.textEdit)
        lay.addWidget(self.button)

        self.button.clicked.connect(self.filePrintPdf)

    def filePrintPdf(self): 
        fn, _ = QFileDialog.getSaveFileName(self, "Export PDF", None, "PDF files (.pdf);;All Files ()") 
        if fn != "": 
            if QFileInfo(fn).suffix() == "": fn += '.pdf' 
        printer = QPrinter(QPrinter.HighResolution) 
        printer.setOutputFormat(QPrinter.PdfFormat) 
        printer.setOutputFileName(fn) 
        self.textEdit.document().print_(printer)

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python已停止在PyQt应用程序中工作 - Python has stopped working in PyQt application PyQt5 GUI出口上的“ Python停止工作” - “Python has stopped working” on PyQt5 GUI exit 实例化 QStyledItemDelegate 的子类两次时出现“Python 已停止工作”错误 - "Python has stopped working" error when instantiating subclass of QStyledItemDelegate twice getSaveFileName,getOpenFileName向我显示要保存或打开的文件,但无法保存该文件或在python QT中打开该文件 - getSaveFileName , getOpenFileName shows me the file to save or open, but cannot save the file or open the file in python QT python pyqt5 将文件名添加到 getSaveFileName - python pyqt5 add file name to getSaveFileName Python 已停止工作 - Python has stopped working PyQt4:使用QFileDialog()保存文件而不是QFileDialog()。getSaveFileName() - PyQt4: Using QFileDialog() to save files not QFileDialog().getSaveFileName() 通过cmd打开python.exe时已停止工作 - python.exe has stopped working when opened via cmd 使用PyDev Eclipse插件时“Python已停止工作” - “Python has stopped working” when using PyDev Eclipse plugin 使用python与MS Word打开pdf文件时,如何抑制“ Microsoft PDF Reflow已停止工作”错误? - How to suppress the “Microsoft PDF Reflow has stopped working” error when using python to open pdf file with MS Word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM