Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';保存';和';另存为';使用QFileDialog.getSaveFile()_Python_Python 3.x_Pyqt_Pyqt5_Qfiledialog - Fatal编程技术网

Python ';保存';和';另存为';使用QFileDialog.getSaveFile()

Python ';保存';和';另存为';使用QFileDialog.getSaveFile(),python,python-3.x,pyqt,pyqt5,qfiledialog,Python,Python 3.x,Pyqt,Pyqt5,Qfiledialog,我正在克隆记事本。虽然保存文件很简单,但我有点被以下问题困扰: QFileDialog.getSavefile()始终提示用户保存文件,即使该文件先前已保存且未对其进行任何更改。如果未对文件进行任何更改,如何使记事本智能化以忽略保存命令?就像windows中真正的记事本一样 以下是从我的项目中提取的保存函数: def save_file(self): """ Saves the user's work. :return: True if th

我正在克隆记事本。虽然保存文件很简单,但我有点被以下问题困扰:

QFileDialog.getSavefile()始终提示用户保存文件,即使该文件先前已保存且未对其进行任何更改。如果未对文件进行任何更改,如何使记事本智能化以忽略保存命令?就像windows中真正的记事本一样

以下是从我的项目中提取的保存函数:

def save_file(self):
    """
    Saves the user's work.
    :return: True if the saving is successful. False if otherwise
    """
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    file_name, _ = QFileDialog.getSaveFileName(self,"Save File","","All Files(*);;Text Files(*.txt)",options = options)
    if file_name:
        f = open(file_name, 'w')
        text = self.ui.textEdit.toPlainText()
        f.write(text)
        self.setWindowTitle(str(os.path.basename(file_name)) + " - Notepad Alpha")
        f.close()
        return True
    else:
        return False

您的问题与QFileDialog无关,而是与您的程序逻辑有关

可以使用变量存储当前文件名,在开始时将其保留为“无”。然后创建两个不同的函数,一个用于“保存”(如果设置了文件名,将尝试保存文件),另一个用于“另存为”(将始终显示文件对话框)

也可以考虑使用该属性来设置/知道(并且让用户知道)文档是否需要保存:

class Notepad(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('New document - Notepad Alpha[*]')
        fileMenu = self.menuBar().addMenu('File')
        saveAction = fileMenu.addAction('Save')
        saveAction.triggered.connect(self.save)
        saveAsAction = fileMenu.addAction('Save as...')
        saveAsAction.triggered.connect(self.saveAs)

        self.editor = QtWidgets.QTextEdit()
        self.setCentralWidget(self.editor)
        self.editor.document().modificationChanged.connect(self.setWindowModified)
        self.fileName = None

    def save(self):
        if not self.isWindowModified():
            return
        if not self.fileName:
            self.saveAs()
        else:
            with open(self.fileName, 'w') as f:
                f.write(self.editor.toPlainText())

    def saveAs(self):
        if not self.isWindowModified():
            return
        options = QtWidgets.QFileDialog.Options()
        options |= QtWidgets.QFileDialog.DontUseNativeDialog
        fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, 
            "Save File", "", "All Files(*);;Text Files(*.txt)", options = options)
        if fileName:
            with open(fileName, 'w') as f:
                f.write(self.editor.toPlainText())
            self.fileName = fileName
            self.setWindowTitle(str(os.path.basename(fileName)) + " - Notepad Alpha[*]")