title: pyqt5 closing a child window from a parent window
body: <p>I have created 2 windows, <code>win1</code> and <code>win2</code>, using qt designer and have added two buttons: <code>btn_open_win2</code> to open <code>win2</code> from <code>win1</code> and <code>btn_close</code> to close <code>win2</code>.</p>
  <p>When I run <code>win2</code> directly the <code>close</code> method works correctly but when I run <code>win1</code> and call win2 from it, the <code>close</code> button on <code>win2</code> doesn't operate correctly.</p>
  <p>I know that the problem arises from the point that win2 has been defined inside the <code>if __name__ =='__main__'</code>  part as shown in the below codes but I can't solve this problem.</p>
  <p>win1:</p>
  <pre class="lang-py prettyprint-override"><code>from PyQt5 import QtCore, QtGui, QtWidgets
  from PyQt5.QtWidgets import QApplication, QMainWindow
  
  class Ui_Win1(object):
      def setupUi(self, MainWindow):
          MainWindow.setObjectName(&quot;Win1&quot;)
          MainWindow.resize(640, 480)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName(&quot;centralwidget&quot;)
          self.btn_open_win2 = QtWidgets.QPushButton(self.centralwidget)
          self.btn_open_win2.setGeometry(QtCore.QRect(250, 170, 131, 101))
          self.btn_open_win2.setObjectName(&quot;btn_open_win2&quot;)
          self.btn_open_win2.clicked.connect(self.on_open_win2)
          MainWindow.setCentralWidget(self.centralwidget)
          self.statusbar = QtWidgets.QStatusBar(MainWindow)
          self.statusbar.setObjectName(&quot;statusbar&quot;)
          MainWindow.setStatusBar(self.statusbar)
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, &quot;Win1&quot;))
          self.btn_open_win2.setText(_translate(&quot;MainWindow&quot;, &quot;open win2&quot;))
  
      def on_open_win2(self):
          from win2 import Ui_Win2
          self.win = QMainWindow()
          self.ui = Ui_Win2()
          self.ui.setupUi(self.win)
          self.win.show()
  
  if __name__ == '__main__':
      import sys
      app = QApplication(sys.argv)
      win1 = QMainWindow()
      ui = Ui_Win1()
      ui.setupUi(win1)
      win1.show()
      sys.exit(app.exec_())
  </code></pre>
  <p>win2:</p>
  <pre class="lang-py prettyprint-override"><code>from PyQt5 import QtCore, QtGui, QtWidgets
  from PyQt5.QtWidgets import QApplication, QMainWindow
  
  class Ui_Win2(object):
      def setupUi(self, MainWindow):
          MainWindow.setObjectName(&quot;Win2&quot;)
          MainWindow.resize(640, 480)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName(&quot;centralwidget&quot;)
          self.btn_close = QtWidgets.QPushButton(self.centralwidget)
          self.btn_close.setGeometry(QtCore.QRect(260, 180, 131, 101))
          self.btn_close.setObjectName(&quot;btn_close&quot;)
          self.btn_close.clicked.connect(self.on_close)
          MainWindow.setCentralWidget(self.centralwidget)
          self.statusbar = QtWidgets.QStatusBar(MainWindow)
          self.statusbar.setObjectName(&quot;statusbar&quot;)
          MainWindow.setStatusBar(self.statusbar)
          self.retranslateUi(MainWindow)
          QtCore.QMetaObject.connectSlotsByName(MainWindow)
  
      def retranslateUi(self, MainWindow):
          _translate = QtCore.QCoreApplication.translate
          MainWindow.setWindowTitle(_translate(&quot;MainWindow&quot;, &quot;Win2&quot;))
          self.btn_close.setText(_translate(&quot;MainWindow&quot;, &quot;close&quot;))
  
      def on_close(self):
          # win2 = QMainWindow()
          # ui = Ui_Win2()
          # ui.setupUi(win2)
          win2.close()
  
  if __name__ == '__main__':
      import sys
      app = QApplication(sys.argv)
      win2 = QMainWindow()
      ui = Ui_Win2()
      ui.setupUi(win2)
      win2.show()
      sys.exit(app.exec_())
  </code></pre>