如何从文本编辑中保存文本

时间:2019-04-17 19:32:39

标签: python-3.x pyqt pyqt5

帮帮我,我想完成此代码,并使用Qt小部件从PyQt5中的文本编辑器中保存文本文件。 Q文件对话框.get保存文件名。帮我。我想在文本编辑器中编写文本并将其保存,但我不知道如何完成Save__File函数。

代码

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(407, 297)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.Text__Edit = QtWidgets.QTextEdit(self.centralwidget)
        self.Text__Edit.setObjectName("Text__Edit")
        self.verticalLayout.addWidget(self.Text__Edit)
        self.OP_Button = QtWidgets.QPushButton(self.centralwidget)
        self.OP_Button.setObjectName("OP_Button")
        self.verticalLayout.addWidget(self.OP_Button)
        self.SV_Button = QtWidgets.QPushButton(self.centralwidget)
        self.SV_Button.setObjectName("SV_Button")
        self.verticalLayout.addWidget(self.SV_Button)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 407, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.retranslateUi(MainWindow)
        self.OP_Button.clicked.connect(self.Open__File)
        self.SV_Button.clicked.connect(self.Save__File)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.OP_Button.setText(_translate("MainWindow", "OpenText"))
        self.SV_Button.setText(_translate("MainWindow", "SaveText"))

    def Open__File(self):
        O__File = QtWidgets.QFileDialog.getOpenFileName(None,'OpenTextFile','/')
        if O__File[0]:
            in__data = open(O__File[0],'r')
            with in__data:
                Text__ = in__data.read()
                self.Text__Edit.setText(Text__)    

    def Save__File(self):
        S__File = QtWidgets.QFileDialog.getSaveFileName(None,'SaveTextFile','/')


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我不熟悉PyOt,但是我做了一些研究并找到了解决方案。尽管我测试了代码,但可能会有更好的方法可以正常工作。

解决方案

您可以使用toPlainText()来访问QTextEdit中的文本。

这是我的方式。

def Save__File(self):
    # S_File will get the directory path and extension.
    S__File = QtWidgets.QFileDialog.getSaveFileName(None,'SaveTextFile','/', "Text Files (*.txt)")

    # This will let you access the test in your QTextEdit
    Text = self.Text__Edit.toPlainText()

    # This will prevent you from an error if pressed cancel on file dialog.
    if S__File[0]: 
       # Finally this will Save your file to the path selected.
       with open(S__File[0], 'w') as file:
       file.write(Text)

我希望这会对您有所帮助。