Suppose we have two windows, one with a button, the parent, and one empty, the child. Codes for parent and child are:
test_parent.py:
from PyQt5 import QtCore, QtGui, QtWidgets
from test_child import Ui_child
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(270, 208)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 90, 89, 25))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.open_child)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def open_child(self):
self.child = QtWidgets.QMainWindow()
self.ui = Ui_child()
self.ui.setupUi(self.child)
self.child.show()
MainWindow.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
and test_child.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_child(object):
def setupUi(self, child):
child.setObjectName("child")
child.resize(275, 176)
self.centralwidget = QtWidgets.QWidget(child)
self.centralwidget.setObjectName("centralwidget")
child.setCentralWidget(self.centralwidget)
self.retranslateUi(child)
QtCore.QMetaObject.connectSlotsByName(child)
def retranslateUi(self, child):
_translate = QtCore.QCoreApplication.translate
child.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_child()
ui.setupUi(child)
child.show()
sys.exit(app.exec_())
With this code, when the user clicks the button, the parent is hiden and the child is shown. Now I want to show again the parent when the users closes the child. How can I do this?