A
First, you don't really want to "hand value to a variable from another module," which you want to modify an instance attribute of an object whose class is defined in another module. It really doesn't matter where the class is defined, the important thing is the object otra Which instances from it. The problem is that you assign as text QLineEdit variable value in only the __init__, this makes when you try class with self.otra = Main2() during the initialization of the text QlineEdit become the value of self.variable at this moment, but this does not create any link between the two, if later it is modified self.variable text QlineEdit It's not gonna change.You could just modify the text QLineEdit of otra directly:self.otra.lineEdit.setText(self.lineEdit_0.text())
But if you want to use an instance attribute, the solution is to use a property that allows you to modify QLineEdit when a new value is assigned to it in the "setter":import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Main2(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.resize(300, 200)
self.centralwidget = QtWidgets.QWidget(self)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 84, 260, 32))
self.setCentralWidget(self.centralwidget)
self._variable = None # Variable "privada", de uso interno.
@property
def variable(self):
return self._variable
@variable.setter
def variable(self, text):
self._variable = text
self.lineEdit.setText(text)
class Main(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(self)
self.boton = QtWidgets.QPushButton(self.centralwidget, text="Aceptar")
self.boton.setGeometry(QtCore.QRect(370, 280, 88, 34))
self.lineEdit_0 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_0.setGeometry(QtCore.QRect(210, 240, 411, 32))
self.setCentralWidget(self.centralwidget)
self.otra = Main2()
self.boton.clicked.connect(self.dato)
def dato(self):
self.otra.variable = self.lineEdit_0.text()
self.otra.show()
if name == "main":
app = QtWidgets.QApplication(sys.argv)
m = Main()
m.show()
sys.exit(app.exec_())
You could even create your own signal and emit it when the attribute is modified, signal that you can connect to the slots as you wish later:import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Main2(QtWidgets.QMainWindow):
variable_changed = QtCore.pyqtSignal()
def __init__(self):
super().__init__()
self.resize(300, 200)
self.centralwidget = QtWidgets.QWidget(self)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 84, 260, 32))
self.setCentralWidget(self.centralwidget)
self._variable = None
self.variable_changed.connect(self.on_changed)
@property
def variable(self):
return self._variable
@variable.setter
def variable(self, text):
self._variable = text
self.variable_changed.emit()
@QtCore.pyqtSlot()
def on_changed(self):
self.lineEdit.setText(self._variable)
However, the association is not reciprocal, i.e. if a value is reassigned to the attribute the content of the QlineEdit changes, but if the QLineEdit the attribute is not modified. If you want this to happen, you need to connect the signal. http://doc.qt.io/qt-5/qlineedit.html#textChanged a slot that takes care of properly modifying the attribute, a possibility is:import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Main2(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.resize(300, 200)
self.centralwidget = QtWidgets.QWidget(self)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(20, 84, 260, 32))
self.setCentralWidget(self.centralwidget)
self._variable = None # Variable "privada", de uso interno.
self.lineEdit.textChanged.connect(self._lineEdit_changed)
@property
def variable(self):
return self._variable
@variable.setter
def variable(self, text):
self.lineEdit.setText(text)
@QtCore.pyqtSlot()
def _lineEdit_changed(self):
self._variable = self.lineEdit.text()
Main2 it is defined in the same module to facilitate reproduction but as commented it is irrelevant that it is declared in another module and the amounts. You must not confuse the concept of module with class and class/object concepts that belong to the object-oriented programming paradigm. A large module is just a file that allows you to store code and structure our application. In one module we can define multiple functions, classes, variables, etc and we can run it directly as a script or import it into another module to reuse the code defined in it.