QLineEdit 在几个屏幕后被删除

QLineEdit being deleted after a couple of screens

我正在使用 QLineEdit,然后需要在 PyQt 中的 2 个屏幕后引用它。但是,当我引用它时,我总是不断收到此错误

Traceback (most recent call last): File "[filename]", line 227, in passwordmissing2ndscreenverify P1=self.entrypasswordmissingscreen1.text() RuntimeError: wrapped C/C++ object of type QLineEdit has been deleted

这是我第一次使用 Qt 创建复杂的程序,这些程序引用了之前来自多个屏幕的内容,所以如果有人可以提供帮助,我们将不胜感激。

下面是该部分的代码:

def passwordmissing1stscreen(self):
    #layouts
    self.mainLayout=QtWidgets.QVBoxLayout()
    self.secondaryLayout=QtWidgets.QHBoxLayout()

    #Labels
    self.passwordMissing1stScreenlabel= QtWidgets.QLabel("Please enter the new password")

    #buttons
    self.cancelbutton=QtWidgets.QPushButton("Cancel", self)
    self.okButton=QtWidgets.QPushButton("OK", self)
    #input
    self.entrypasswordmissingscreen1 = QtWidgets.QLineEdit()
    P1 = self.entrypasswordmissingscreen1.text()
    #conections
    self.cancelbutton.clicked.connect(self.exit)
    self.okButton.clicked.connect(self.passwordmissing2ndscreen)

    #add to layouts
    self.mainLayout.addWidget(self.passwordMissing1stScreenlabel)
    self.mainLayout.addWidget(self.entrypasswordmissingscreen1)
    self.secondaryLayout.addWidget(self.okButton)
    self.secondaryLayout.addWidget(self.cancelbutton)
    self.mainLayout.addLayout(self.secondaryLayout)


    #display screen#
    self.mainViewWidget = QtWidgets.QWidget()
    self.mainViewWidget.setLayout(self.mainLayout)
    self.setCentralWidget(self.mainViewWidget)



def passwordmissing2ndscreen(self):
    #layouts
    self.mainLayout=QtWidgets.QVBoxLayout()
    self.secondaryLayout=QtWidgets.QHBoxLayout()

    #Labels
    self.passwordMissing2ndScreenlabel= QtWidgets.QLabel("Please enter the new password again")

    #buttons
    self.cancelbutton=QtWidgets.QPushButton("Cancel", self)
    self.okButton=QtWidgets.QPushButton("OK", self)

    #input
    self.entrypasswordmissingscreen2=QtWidgets.QLineEdit()


    #add to layouts
    self.mainLayout.addWidget(self.passwordMissing2ndScreenlabel)
    self.mainLayout.addWidget(self.entrypasswordmissingscreen2)
    self.secondaryLayout.addWidget(self.okButton)
    self.secondaryLayout.addWidget(self.cancelbutton)
    self.mainLayout.addLayout(self.secondaryLayout)
    #conections
    self.cancelbutton.clicked.connect(self.exit)
    self.okButton.clicked.connect(self.passwordmissing2ndscreenverify)

    #display screen#
    self.mainViewWidget = QtWidgets.QWidget()
    self.mainViewWidget.setLayout(self.mainLayout)
    self.setCentralWidget(self.mainViewWidget)

def passwordmissing2ndscreenverify(self):
    P1=self.entrypasswordmissingscreen1.text()
    P2=self.entrypasswordmissingscreen2.text()
    print (P1)
    print (P2)

好吧,在与朋友和其他程序员讨论之后,我们找到了一个完美的解决方案,而不是在 passwordmissing1stscreen 中使用 P1 = self.entrypasswordmissingscreen1.text(),而是将 self.P1.entrypasswordmissingscreen1.text() 放在 passwordmissing2ndscreen 中,并且所有引用都指向 P1 改为 self.P1