Hello good morning ,I am learning how to use temporary files to save and load information in an application and I am experiencing problems when loading the information for the second time when I open Qdialog Win1 with out close the app,could you help me?
from PySide6.QtWidgets import QApplication,QMainWindow,QPushButton,QSpinBox,QDialog,QDialogButtonBox,QFormLayout
import tempfile,ast
import sys
app=QApplication(sys.argv)
data={'num1':2,'num2':4}
class Ventana_Principal(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(400,200)
self.initUi()
self.create_temp_file()
def initUi(self):
self.button1=QPushButton()
self.button1.clicked.connect(self.first_wnd)
self.setCentralWidget(self.button1)
def create_temp_file(self):
self.data=str(data)
self.temporary_file=tempfile.TemporaryFile('w+')
self.temporary_file.write(self.data)
def first_wnd(self):
window1=Win1(self.temporary_file)
def closeEvent(self, event):
self.temporary_file.close()
class Win1(QDialog):
def __init__(self,temp_file):
super().__init__()
self.setFixedSize(180,120)
self.temporary_file=temp_file
self.qspin1=QSpinBox()
self.qspin1.setFixedSize(50,25)
self.qspin2=QSpinBox()
self.qspin2.setFixedSize(50,25)
self.layout1=QFormLayout(self)
self.layout1.setHorizontalSpacing(20)
self.layout1.setVerticalSpacing(50)
#self.layout1.setContentsMargins(0,0,13)
self.botones=QDialogButtonBox()
#self.botones.setGeometry(100,20,50,25)
self.botones=QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.botones.accepted.connect(self.update_data)
self.botones.rejected.connect(self.cancelar)
self.layout1.addRow(self.qspin1,self.qspin2)
self.layout1.addRow(self.botones)
self.load_data()
self.exec()
def load_data(self):
self.temporary_file.seek(0)
self.data_back=ast.literal_eval(self.temporary_file.read())
self.qspin1.setValue(float(self.data_back['num1']))
self.qspin2.setValue(float(self.data_back['num2']))
print(self.data_back)
def update_data(self):
self.data_back['num1']=self.qspin1.value()
self.data_back['num2']=self.qspin2.value()
self.data_in=str(self.data_back)
self.temporary_file.write(self.data_in)
self.accept()
def cancelar(self):
self.reject()
wnd=Ventana_Principal()
wnd.show()
app.exec()