如何保存单个值并重新打开它们以备后用?

时间:2019-07-01 18:50:48

标签: python pyqt pyqt5 qt-designer

我创建了一个仅将两个值相加的应用程序。我的目标是将所有这些值都保存到文件中,以便以后可以重新插入。现在,我只能将其保存为一个值,也可以重新打开保存该文件的文件,然后将其重新插入。

from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QPushButton


class MyWindow(QtWidgets.QMainWindow, QPushButton):
    def __init__(self):
        super(MyWindow, self).__init__()
        uic.loadUi('File_Stuff.ui', self)
        self.setWindowTitle("File Stuff")
        self.Add.clicked.connect(self.Math)

        openFile = QtWidgets.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.triggered.connect(self.file_open)

        saveFile = QtWidgets.QAction("&Save File", self)
        saveFile.setShortcut("Ctrl+S")
        saveFile.triggered.connect(self.file_save)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)
        fileMenu.addAction(saveFile)


    def Math(self):
        a = self.LE1.text()
        b = self.LE2.text()
        answer = float(a) + float(b)
        answer2 = str(answer)
        self.TE.setText(answer2)

    def file_open(self):
        name = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File')
        file = open(name[0], 'r')

        with file:
            text = file.read()
            self.LE1.setText(text)
 # if I put self.LE2.setText(text), it inserts both values into both Line Edits

    def file_save(self):
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Select File')
        file = open(name[0], 'w')
        text = self.LE1.text()
        file.write(text)
        file.close()
 # if I put text2 = self.LE2.text() and file.write(text2), it saves them together

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

当我重新打开两个值保存到的文件时,我无法分别调用这两个值。

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QPushButton, QWidget, QLineEdit, QTextEdit, QGridLayout 


class MyWindow(QtWidgets.QMainWindow, QPushButton):
    def __init__(self):
        super(MyWindow, self).__init__()

#        uic.loadUi('File_Stuff.ui', self)
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.Add = QPushButton("Add")
        self.LE1 = QLineEdit()
        self.LE2 = QLineEdit() 
        self.TE  = QTextEdit()


        self.setWindowTitle("File Stuff")
        self.Add.clicked.connect(self.Math)

        openFile = QtWidgets.QAction("&Open File", self)
        openFile.setShortcut("Ctrl+O")
        openFile.triggered.connect(self.file_open)

        saveFile = QtWidgets.QAction("&Save File", self)
        saveFile.setShortcut("Ctrl+S")
        saveFile.triggered.connect(self.file_save)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)
        fileMenu.addAction(saveFile)

        layout = QGridLayout(centralWidget)
        layout.addWidget(self.LE1)
        layout.addWidget(self.LE2)
        layout.addWidget(self.TE)
        layout.addWidget(self.Add)

    def Math(self):
        a = self.LE1.text()
        b = self.LE2.text()
        answer = float(a) + float(b)
        answer2 = str(answer)
        self.TE.setText(answer2)

    def file_open(self):
        name = QtWidgets.QFileDialog.getOpenFileName(self, 'Select File')
        file = open(name[0], 'r')

        with file:
#            text = file.read()
            myList = file.read().split()                                         # <---
            self.LE1.setText(myList[0])                                          # <---
            self.LE2.setText(myList[1])                                          # <---

 # if I put self.LE2.setText(text), it inserts both values into both Line Edits

    def file_save(self):
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Select File')
        file = open(name[0], 'w')

        text = "{}\n{}".format(self.LE1.text(), self.LE2.text())                   # <---

        file.write(text)
        file.close()
 # if I put text2 = self.LE2.text() and file.write(text2), it saves them together

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())