Home > Mobile >  save array of object in file in python
save array of object in file in python

Time:10-04

I want to save array of my object in file in python and I test pickle, numpy,..but it doesn't work. my class that I want to save instance of it in file is:

class pesrson():
def __init__(self,name,centralwidget):
    self.name=name
    self.centralwidget=centralwidget
def config(self):
    self.frame_eployee = QFrame(self.centralwidget)
    self.frame_eployee.setObjectName(u"frame_eployee")
    self.frame_eployee.setGeometry(QRect(20, 590, 321, 51))
    self.frame_eployee.setStyleSheet(u"color:rgb(85, 170, 255)")
    self.frame_eployee.setFrameShape(QFrame.StyledPanel)
    self.frame_eployee.setFrameShadow(QFrame.Raised)
    self.label_fix_logo_user = QLabel(self.frame_eployee)
    self.label_fix_logo_user.setObjectName(u"label_fix_logo_user")
    self.label_fix_logo_user.setGeometry(QRect(10, 0, 41, 41))
    self.label_fix_logo_user.setPixmap(QPixmap(u"D:/V2.0.0/Programme/qt/UI design/picture/user.svg"))
    self.label_name_employee = QLabel(self.frame_eployee)
    self.label_name_employee.setObjectName(u"label_name_employee")
    self.label_name_employee.setGeometry(QRect(60, 10, 101, 21))
    self.label_name_employee.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                           "font: 25 10pt \"Segoe UI Light\";")
    self.label_tim_login = QLabel(self.frame_eployee)
    self.label_tim_login.setObjectName(u"label_tim_login")
    self.label_tim_login.setGeometry(QRect(190, 10, 61, 21))
    self.label_tim_login.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                       "font: 25 10pt \"Segoe UI Light\";")
    self.label_time_logOut = QLabel(self.frame_eployee)
    self.label_time_logOut.setObjectName(u"label_time_logOut")
    self.label_time_logOut.setGeometry(QRect(250, 10, 61, 21))
    self.label_time_logOut.setStyleSheet(u"color: rgb(255, 255, 255);\n"
                                         "font: 25 10pt \"Segoe UI Light\";")
    self.label_name_employee.setText(QCoreApplication.translate("MainWindow", self.name, None))
    self.label_tim_login.setText(
        QCoreApplication.translate("MainWindow", u"<html><head/><body><p align=\"center\">08:34</p></body></html>",
                                   None))
    self.label_time_logOut.setText(
        QCoreApplication.translate("MainWindow", u"<html><head/><body><p align=\"center\">08:34</p></body></html>",
                                   None))
    return self.frame_eployee

I create this object with this code:

 p=pesrson("faezeh",self.ui.centralwidget)
    self.ui.verticalLayout.addWidget(p.config())
    self.tree.append(p)

now I want to save this array then read them and can use all attributes. this error is when I use json :

TypeError: Object of type person is not JSON serializable
<employee.pesrson object at 0x000001DD17C7B7F0>

this error when I use pickle:

Possibly unsupported): cannot pickle 'PySide2.QtWidgets.QFrame' object

CodePudding user response:

you could use the pickle module in the standard library and also you could use a database instead of a file. I think it would fit your problem.

  • Related