Python has stopped working / AttributeError: 'Ui_Form' object has no attribute 'lineEdit'
-
I want to set the label in B.py to whatever the user typed in A.py self.lineEdit. However, when I clicked the button in A.py, the application crashed and display this message: Python has stopped working. Then I ran B.py to see what is the problem and this error shows: AttributeError: 'Ui_Form' object has no attribute 'lineEdit'.
A.py from PyQt6 import QtCore, QtGui, QtWidgets import B class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(203, 146) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setSpacing(0) self.verticalLayout.setObjectName("verticalLayout") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setObjectName("lineEdit") self.verticalLayout.addWidget(self.lineEdit) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) self.pushButton.clicked.connect(self.enter) self.pushButton.clicked.connect(Form.close) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "Enter")) def enter(self): self.lineedit_txt = self.lineEdit.text() self.window_login1 = QtWidgets.QWidget() self.ui_login1 = B.Ui_Form() self.ui_login1.setupUi2(self.window_login1) self.window_login1.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec()) B.py from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi2(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setObjectName("verticalLayout") self.label = QtWidgets.QLabel(Form) self.label.setText("") self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) self.output() def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) def output(self): import A obj = A.Ui_Form() obj.enter() txt_for_label = obj.lineedit_txt self.label.setText(txt_for_label) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi2(Form) Form.show() sys.exit(app.exec()) -
I want to set the label in B.py to whatever the user typed in A.py self.lineEdit. However, when I clicked the button in A.py, the application crashed and display this message: Python has stopped working. Then I ran B.py to see what is the problem and this error shows: AttributeError: 'Ui_Form' object has no attribute 'lineEdit'.
A.py from PyQt6 import QtCore, QtGui, QtWidgets import B class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(203, 146) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setSpacing(0) self.verticalLayout.setObjectName("verticalLayout") self.lineEdit = QtWidgets.QLineEdit(Form) self.lineEdit.setObjectName("lineEdit") self.verticalLayout.addWidget(self.lineEdit) self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) self.pushButton.clicked.connect(self.enter) self.pushButton.clicked.connect(Form.close) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "Enter")) def enter(self): self.lineedit_txt = self.lineEdit.text() self.window_login1 = QtWidgets.QWidget() self.ui_login1 = B.Ui_Form() self.ui_login1.setupUi2(self.window_login1) self.window_login1.show() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec()) B.py from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi2(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.verticalLayout = QtWidgets.QVBoxLayout(Form) self.verticalLayout.setObjectName("verticalLayout") self.label = QtWidgets.QLabel(Form) self.label.setText("") self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) self.output() def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) def output(self): import A obj = A.Ui_Form() obj.enter() txt_for_label = obj.lineedit_txt self.label.setText(txt_for_label) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi2(Form) Form.show() sys.exit(app.exec())@jerap
Let's start with:# In `A.enter()` self.ui_login1 = B.Ui_Form() # In `B.output()` import A obj = A.Ui_Form() obj.enter()That does not look very promising, crash-wise....
Next: when as above in
B.output()you do goimport A obj = A.Ui_Form() obj.enter()you have never gone
A.Ui_Form.setupUi()so yourA.Formdoes not have anyself.lineEdit, doubtless hence the error message.Don't know what you're trying to do with this convolution, but you'd better sort your instances of
A&Bout because I suspect you're failing to understand correctly accessing existing instances and creating new ones afresh without understanding. -
@jerap
Let's start with:# In `A.enter()` self.ui_login1 = B.Ui_Form() # In `B.output()` import A obj = A.Ui_Form() obj.enter()That does not look very promising, crash-wise....
Next: when as above in
B.output()you do goimport A obj = A.Ui_Form() obj.enter()you have never gone
A.Ui_Form.setupUi()so yourA.Formdoes not have anyself.lineEdit, doubtless hence the error message.Don't know what you're trying to do with this convolution, but you'd better sort your instances of
A&Bout because I suspect you're failing to understand correctly accessing existing instances and creating new ones afresh without understanding. -
@JonB
What I want to do is to set the text ofB.self.labelto whatever the user inputs inA.self.lineEdit.@jerap
You need to start by sorting out your architecture before that. How are A and B related? Which one shows which one? Is only one at a time supposed to be visible, are they both supposed to be visible all the time? I suggest you don't worry about the label till you have that sorted out.Then they way to pass information between one and the other will be clearer.