简体   繁体   中英

Trying to save data in pyqt5

I am trying to save TEXT of a list in pyqt5. The text is coming but for only one line, I want the entire string inside the listbox to be saved inside the saved.txt folder. Whenever I uses for loop the window dissappears automatically. Please guys explain me your answers throughly I am finding pyqt5 difficult.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, \
    QListWidget, QListWidgetItem, QLineEdit
import sys



class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.x = 200
        self.y = 200
        self.width = 500
        self.length = 500
        self.setGeometry(self.x, self.y, self.width, self.length)
        self.setWindowTitle("Stock managment")

        self.iniTUI()
    # I tried this
    def save(self):
        f = open("saved.txt", "w")
        f.write(f"{self.item4.text()}\n")

        f.close()



    def update(self):
        self.label.adjustSize()



    def take_inputs(self):
        self.name, self.done1 = QtWidgets.QInputDialog.getText(
            self, 'Add Item to List', 'Enter The Item you want in the list:')
        self.roll, self.done2 = QtWidgets.QInputDialog.getInt(
            self, f'Quantity of {str(self.name)}', f'Enter Quantity of {str(self.name)}:')

        
        if self.done1 and self.done2:
            self.item4 = QListWidgetItem(f"{str(self.name)}              Quantity{self.roll} ")
            # This is the text I want to save.
            self.list_widget.addItem(self.item4)
            self.list_widget.setCurrentItem(self.item4)
            self.pick = pickle.dump(self.item4.text())
            self.save()




    def iniTUI(self):
        # buttons
        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("+")
        self.b1.move(450, 100)
        self.b1.resize(50, 25)
        self.b1.clicked.connect(self.take_inputs)

        # list
        self.list_widget = QListWidget(self)

        self.list_widget.setGeometry(120, 100, 250, 300)
        #pickle.dump(self.list_widget)




        #self.nameLineEdit = QLineEdit("Type Here", self)
        #self.nameLineEdit.setGeometry(80, 80, 150, 20)
        #self.nameLineEdit.move(50, 50)

def window():
    apk = QApplication(sys.argv)
    win = MyWindow()

    win.show()
    sys.exit(apk.exec_())


window()

I will not explain why the OP's code is wrong as it is confusing, instead I will show how to save the text displayed in a QListWidget to a file.

The procedure is to iterate over the QListWidgetItem and then obtain the texts:

with open("Saved.text") as f:
    for i in range(self.list_widget.count()):
        item = self.list_widget.item(i)
        text = item.text() if item is not None else ""
        f.write("%s\n" % text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM