PyQt: How can I access the attributes in a class method from another file

Viewed 15
A.py

from PyQt6 import QtCore, QtGui, QtWidgets


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.open_other_window)
        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 open_other_window(self):
        import B

        self.window = QtWidgets.QWidget()
        self.ui = B.Ui_Form()
        self.ui.setupUi(self.window)
        self.window.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())

What I simply want to do is simply access the attributes of A.py like self.lineEdit because I want to set the text of B.self.label to whatever the user inputs in A.self.lineEdit.

B.py

from PyQt6 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(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("Default Text")
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

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())

Here are the things that I've done so far:

B.py
    def change_text(self):
        import A

        win = QtWidgets.QWidget()
        obj = A.Ui_Form()
        obj.setupUi(win)

        txt = obj.lineEdit.text()
        self.label.setText(txt)

And:

A.py

win = QtWidgets.QWidget()
obj = Ui_Form()
obj.setupUi(win)

input_txt = obj.lineEdit.text()

B.py 

    def change_text(self):
        import A

        
        txt = A.input_txt
        self.label.setText(txt)

Both show the same result, when I hit the A button, it redirects me to B. But then the text isn't even changed to A.self.lineEdit text, however the default text written "Default text" disappears.

0 Answers
Related