1

I hava a QMainWindow which is being started by the following code in a file called main.py,

if __name__ == '__main__':
app = QApplication(sys.argv)
window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(window)

window.show()
sys.exit(app.exec_())

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

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

The code to open the second window is contained in the code for the first window.

    def userWindow(self):
       window = QtWidgets.QMainWindow()
       ui = Ui_Select()
       ui.setup_Ui(window)
       window.show()
       return window

Here is a shortened version of the code for my second window

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

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.

I have tried the solutions here:

and I am still at a loss. What am I missing?

GL007
  • 43
  • 1
  • 9

0 Answers0