如何保存单个值并重新打开它们以供以后使用?
Posted
技术标签:
【中文标题】如何保存单个值并重新打开它们以供以后使用?【英文标题】:How to save individual values and reopen them for later use? 【发布时间】:2019-07-01 18:50:48 【问题描述】:我创建了一个简单地将两个值相加的应用。我的目标是将这两个值都保存到一个文件中,以便我以后可以重新插入它们。现在我只能保存一个值,我也可以重新打开保存它的文件并将值重新插入。
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_())
当我重新打开保存它们的文件时,我无法分别调用这两个值。
【问题讨论】:
您忘记发布File_Stuff.ui
它不会让我。所有 ui 文件都有两个行编辑(名为 LE1 和 LE2)和一个文本编辑器(名为 TE)
【参考方案1】:
试试看:
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_())
【讨论】:
它也可以保存为 csv,允许在同一个文件中保存一组以上的值,然后换行符 (/n) 将指示每个“记录”(一组值) 这将是存储此类数据的更典型方式。 @DennisJensen 我不介意,准备好您的决定并将其作为答案发布。 不需要,它只是你的版本略有不同——我提出了这个建议,以防个人对数据做更多的事情。当然,这些值也可以存储为字典(也称为 json 对象),这在很大程度上取决于他们计划长期使用该数据文件做什么 感谢您的帮助,非常感谢! 您是否知道如何调整列表以使其接受多个值(我在一个单独的项目中最多有 9 个)以上是关于如何保存单个值并重新打开它们以供以后使用?的主要内容,如果未能解决你的问题,请参考以下文章