title: How to stop second PyQt5 QMainWindow from closing?
body: <p>I hava a QMainWindow which is being started by the following code in a file called main.py,</p>
  
  <pre><code>if __name__ == '__main__':
  app = QApplication(sys.argv)
  window = QMainWindow()
  ui = Ui_MainWindow()
  ui.setupUi(window)
  
  window.show()
  sys.exit(app.exec_())
  </code></pre>
  
  <p>It starts off the first QMainWindow that has a bunch of different QtWidgets on it. Namely, a QActionButton which when triggered, connects to a method that should open up another QMainWindow, with different QtWidgets on it. This is the code for that, shortened down</p>
  
  <pre><code>class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.setEnabled(True)
      self.actionSwitch_Option.triggered.connect(self.userWindow)
  
    def userWindow(self):
       window = QtWidgets.QMainWindow()
       ui = Ui_Select()
       ui.setup_Ui(window)
       window.show()
       return window
  
    def retranslateUi(self, MainWindow):
      _translate = QtCore.QCoreApplication.translate
      MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  </code></pre>
  
  <p>The code to open the second window is contained in the code for the first window. </p>
  
  <pre><code>    def userWindow(self):
         window = QtWidgets.QMainWindow()
         ui = Ui_Select()
         ui.setup_Ui(window)
         window.show()
         return window
  </code></pre>
  
  <p>Here is a shortened version of the code for my second window</p>
  
  <pre><code>class Ui_Select(QtWidgets.QMainWindow):
    def setup_Ui(self, SelectOption):
      SelectOption.setObjectName("SelectOption")
      SelectOption.resize(414, 325)
      self.userSelectButtonBox = QtWidgets.QDialogButtonBox(SelectOption)
  
    def retranslateUi(self, SelectOption):
      _translate = QtCore.QCoreApplication.translate
  </code></pre>
  
  <p>As soon as the new window is opened, it is immediately closed, leaving just the first window. I have tried many solutions, and read everything relevant that I could find on here. My understanding is that somehow the window is falling out of scope, so it's being garbage collected.</p>
  
  <p>I have tried the solutions here: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/36768033/pyqt-how-to-open-new-window">PyQT: how to open new window</a> </li>
  <li><a href="https://stackoverflow.com/questions/30080927/pop-up-dialog-from-one-button-on-the-main-window-pyqt5">Pop up dialog from one button on the main window, PyQt5</a> </li>
  <li><a href="https://stackoverflow.com/questions/16069713/pyqt-window-closes-immediately-after-opening">PyQt window closes immediately after opening</a> </li>
  </ul>
  
  <p>and I am still at a loss. What am I missing?</p>