为什么我的列表不能保存 QListWidget 中的所有内容?

why can't my list save everything in the QListWidget?

我正在创建一个可以保存每一天的待办事项列表应用程序(如果您进入该应用程序)。似乎有问题。这是我的代码:

import PySide6.QtWidgets as qtw
import PySide6.QtCore as qtc
import sys
import pandas as pd
class UI_MainWindow(qtw.QWidget):
    def __init__(self):
        super(UI_MainWindow, self).__init__()
        self.initUI()
    def initUI(self):
        self.add_item_txt = qtw.QLineEdit(self)
        self.add_item_txt.setGeometry(10,50,311,21)

        self.add_bnt = qtw.QPushButton("add", self)
        self.add_bnt.setToolTip("add the item in the text box to the list")
        self.add_bnt.move(0,100)
        self.add_bnt.resize(self.add_bnt.sizeHint())
        self.add_bnt.clicked.connect(self.add_item)

        self.delete_bnt = qtw.QPushButton("delete", self)
        self.delete_bnt.setToolTip("deletes the item that you are clicking on")
        self.delete_bnt.move(110,100)
        self.delete_bnt.resize(self.delete_bnt.sizeHint())
        self.delete_bnt.clicked.connect(self.delete_item)

        self.clear_bnt = qtw.QPushButton("clear", self)
        self.clear_bnt.setToolTip("add the item in the text box to the list")
        self.clear_bnt.move(220,100)
        self.clear_bnt.resize(self.add_bnt.sizeHint())
        self.clear_bnt.clicked.connect(self.clear_item)

        self.the_lst = qtw.QListWidget(self)
        self.the_lst.setGeometry(0,180,471,251)

        self.save_bnt = qtw.QPushButton("save", self)
        self.save_bnt.setToolTip("save the to-do list for today")
        self.save_bnt.move(0,440)
        self.save_bnt.resize(self.save_bnt.sizeHint())
        self.save_bnt.clicked.connect(self.save)

        self.quit_bnt = qtw.QPushButton("quit", self)
        self.quit_bnt.setToolTip("quits the application")
        self.quit_bnt.move(170,440)
        self.quit_bnt.resize(self.quit_bnt.sizeHint())
        self.quit_bnt.clicked.connect(self.quit)

        self.cal = qtw.QCalendarWidget(self)
        self.cal.move(430,540)


        self.find_bnt = qtw.QPushButton("find", self)
        self.find_bnt.setToolTip("allow you to find a specific to-do list (showed by the date) and show it")
        self.find_bnt.move(360,440)
        self.find_bnt.resize(self.find_bnt.sizeHint())
        self.find_bnt.clicked.connect(self.find_todo)


        self.show()
        self.setWindowTitle("to-do list")
        self.setGeometry(0,0,739,715)
    def add_item(self):
        item = self.add_item_txt.text()

        self.the_lst.addItem(item)

        self.add_item_txt.setText("")

        #self.the_lst.addItem("")

    def delete_item(self):
        clicked = self.the_lst.currentRow()

        self.the_lst.takeItem(clicked)
    
    def clear_item(self):
        self.the_lst.clear()

    def save(self):
        self.todo_list = []
        for the_item in range(self.the_lst.count()-1):
            print("item(s) before: ", self.todo_list)
            self.todo_list.append(self.the_lst.item(the_item).text())
            print("after: ", self.todo_list)

        print("todo list: \n", self.todo_list)
        self.today_date = self.cal.selectedDate()
        save_file_name = self.today_date.toString("yyyy")+"_"+self.today_date.toString("M")+"_"+self.today_date.toString("d")+"todo.csv"
        dt = pd.DataFrame(self.todo_list)
        print("dt:",dt)

    def find_todo(self):
        pass

    def quit(self):
        reply = qtw.QMessageBox.question(self,"Quit?", "are you sure you want to quit?", qtw.QMessageBox.Yes|qtw.QMessageBox.No,qtw.QMessageBox.No)

        if reply == qtw.QMessageBox.Yes:
            quit()

app = qtw.QApplication(sys.argv)
window = UI_MainWindow()
sys.exit(app.exec_())

它将显示如下内容:

并且我想分配一个等于 QListWidget 中每个值的列表。但事情是这样的:

QListWidget:

这是列表等于: ["item","item 1"]

根据@eyllanesc 所说,

import PySide6.QtWidgets as qtw
import PySide6.QtCore as qtc
import sys
import pandas as pd
class UI_MainWindow(qtw.QWidget):
    def __init__(self):
        super(UI_MainWindow, self).__init__()
        self.initUI()
    def initUI(self):
        self.add_item_txt = qtw.QLineEdit(self)
        self.add_item_txt.setGeometry(10,50,311,21)

        self.add_bnt = qtw.QPushButton("add", self)
        self.add_bnt.setToolTip("add the item in the text box to the list")
        self.add_bnt.move(0,100)
        self.add_bnt.resize(self.add_bnt.sizeHint())
        self.add_bnt.clicked.connect(self.add_item)

        self.delete_bnt = qtw.QPushButton("delete", self)
        self.delete_bnt.setToolTip("deletes the item that you are clicking on")
        self.delete_bnt.move(110,100)
        self.delete_bnt.resize(self.delete_bnt.sizeHint())
        self.delete_bnt.clicked.connect(self.delete_item)

        self.clear_bnt = qtw.QPushButton("clear", self)
        self.clear_bnt.setToolTip("add the item in the text box to the list")
        self.clear_bnt.move(220,100)
        self.clear_bnt.resize(self.add_bnt.sizeHint())
        self.clear_bnt.clicked.connect(self.clear_item)

        self.the_lst = qtw.QListWidget(self)
        self.the_lst.setGeometry(0,180,471,251)

        self.save_bnt = qtw.QPushButton("save", self)
        self.save_bnt.setToolTip("save the to-do list for today")
        self.save_bnt.move(0,440)
        self.save_bnt.resize(self.save_bnt.sizeHint())
        self.save_bnt.clicked.connect(self.save)

        self.quit_bnt = qtw.QPushButton("quit", self)
        self.quit_bnt.setToolTip("quits the application")
        self.quit_bnt.move(170,440)
        self.quit_bnt.resize(self.quit_bnt.sizeHint())
        self.quit_bnt.clicked.connect(self.quit)

        self.cal = qtw.QCalendarWidget(self)
        self.cal.move(430,540)


        self.find_bnt = qtw.QPushButton("find", self)
        self.find_bnt.setToolTip("allow you to find a specific to-do list (showed by the date) and show it")
        self.find_bnt.move(360,440)
        self.find_bnt.resize(self.find_bnt.sizeHint())
        self.find_bnt.clicked.connect(self.find_todo)


        self.show()
        self.setWindowTitle("to-do list")
        self.setGeometry(0,0,739,715)
    def add_item(self):
        item = self.add_item_txt.text()

        self.the_lst.addItem(item)

        self.add_item_txt.setText("")

        #self.the_lst.addItem("")

    def delete_item(self):
        clicked = self.the_lst.currentRow()

        self.the_lst.takeItem(clicked)
    
    def clear_item(self):
        self.the_lst.clear()

    def save(self):
        self.todo_list = []
        for the_item in range(self.the_lst.count()):
            print("item(s) before: ", self.todo_list)
            self.todo_list.append(self.the_lst.item(the_item).clone().text())
            print("after: ", self.todo_list)

        print("todo list: \n", self.todo_list)
        self.today_date = self.cal.selectedDate()
        save_file_name = self.today_date.toString("yyyy")+"_"+self.today_date.toString("M")+"_"+self.today_date.toString("d")+"todo.csv"
        dt = pd.DataFrame(self.todo_list)
        print("dt:",dt)

    def find_todo(self):
        pass

    def quit(self):
        reply = qtw.QMessageBox.question(self,"Quit?", "are you sure you want to quit?", qtw.QMessageBox.Yes|qtw.QMessageBox.No,qtw.QMessageBox.No)

        if reply == qtw.QMessageBox.Yes:
            quit()

app = qtw.QApplication(sys.argv)
window = UI_MainWindow()
sys.exit(app.exec_())