0

I have been searching and searching, but cant seem to find any examples that match what I am doing. Essentially I have two QMainWindow UIs that I built using the designer and used the cli tool to convert them to python files. The main window loads perfectly and works as intended. After the user makes some selections and clicks a button, I am wanting a child window to open to perform actions based on the selection in the parent window. The problem is the child window will not open. There are no errors thrown, the window just will not open. Im at a loss and Ive asked some other people and they are also not sure why it isnt opening, so Im hoping the brain trust around here might be able to help me out. Using pseudo code to simplify everything. If I swap out the UIs in __main__ the second UI loads so I know its code is good. If I can get this to work the next thing I am needing is to be able to send a variable over when the window opens, which I think I can do in the setup UI function when I call it. Any and all help is appreciated as I have tried a lot of variations and either get the same results, or python crashes with I click the open child window button.

main.py contents:

from PyQt6 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(500, 100)
        MainWindow.setMinimumSize(QtCore.QSize(500, 100))
        MainWindow.setMaximumSize(QtCore.QSize(500, 100))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lbl_title = QtWidgets.QLabel(self.centralwidget)
        self.lbl_title.setGeometry(QtCore.QRect(0, 0, 500, 25))
        self.lbl_title.setMinimumSize(QtCore.QSize(500, 25))
        self.lbl_title.setMaximumSize(QtCore.QSize(500, 25))
        font = QtGui.QFont()
        font.setFamily("Verdana")
        font.setPointSize(13)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.lbl_title.setFont(font)
        self.lbl_title.setAutoFillBackground(False)
        self.lbl_title.setStyleSheet("background-color: rgb(11, 91, 172);\n"
                                     "font: 13pt \"Verdana\";\n"
                                     "color: white;")
        self.lbl_title.setFrameShape(QtWidgets.QFrame.Shape.Box)
        self.lbl_title.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.lbl_title.setLineWidth(2)
        self.lbl_title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.lbl_title.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
        self.lbl_title.setObjectName("lbl_title")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(29, 30, 442, 66))
        self.widget.setObjectName("widget")
        self.grd_main = QtWidgets.QGridLayout(self.widget)
        self.grd_main.setContentsMargins(0, 0, 0, 0)
        self.grd_main.setObjectName("grd_main")
        self.cb_cb1 = QtWidgets.QComboBox(self.widget)
        self.cb_cb1.setObjectName("cb_cb1")
        self.grd_main.addWidget(self.cb_cb1, 1, 2, 1, 1)
        self.lbl_select = QtWidgets.QLabel(self.widget)
        self.lbl_select.setAlignment(
            QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignTrailing
            | QtCore.Qt.AlignmentFlag.AlignVCenter)
        self.lbl_select.setObjectName("lbl_select")
        self.grd_main.addWidget(self.lbl_select, 1, 0, 1, 2)
        self.btn_load_objects = QtWidgets.QPushButton(self.widget)
        self.btn_load_objects.setObjectName("btn_load_objects")
        self.btn_load_objects.clicked.connect(self.load_object_button_clicked)
        self.grd_main.addWidget(self.btn_load_objects, 0, 3, 1, 1)
        self.btn_openchild = QtWidgets.QPushButton(self.widget)
        self.btn_openchild.setEnabled(False)
        self.btn_openchild.setObjectName("btn_openchild")
        self.btn_openchild.clicked.connect(self.openchild_button_clicked)
        self.grd_main.addWidget(self.btn_openchild, 1, 3, 1, 1)
        self.cb_cb2 = QtWidgets.QComboBox(self.widget)
        self.cb_cb2.setObjectName("cb_cb2")
        self.grd_main.addWidget(self.cb_cb2, 0, 2, 1, 1)
        self.lbl_select2 = QtWidgets.QLabel(self.widget)
        self.lbl_select2.setAlignment(
            QtCore.Qt.AlignmentFlag.AlignRight | QtCore.Qt.AlignmentFlag.AlignTrailing
            | QtCore.Qt.AlignmentFlag.AlignVCenter)
        self.lbl_select2.setObjectName("lbl_select2")
        self.grd_main.addWidget(self.lbl_select2, 0, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.actionExit = QtGui.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "I am the parent application window"))
        self.lbl_title.setText(_translate("MainWindow", "Main Application"))
        self.lbl_select.setText(_translate("MainWindow", "Select an object:"))
        self.btn_load_objects.setText(_translate("MainWindow", "Load"))
        self.btn_openchild.setText(_translate("MainWindow", "Open Child Window"))
        self.lbl_select2.setText(_translate("MainWindow", "Select an option to load:"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        # Get list of cb2
        self.select2_list = <list obtained from file>
        self.cb_cb2.addItems(self.select2_list)

    def load_object_button_clicked(self):
        self.cb_cb1.addItems(<items retrieved using a call with self.cb_cb2.currentText()>)
        self.btn_openchild.setEnabled(True)

    def openchild_button_clicked(self):
        child_window = QtWidgets.QMainWindow()
        child_ui = Ui_ChildWindow()
        child_ui.setupUi(child_window)
        child_window.show()


class Ui_ChildWindow(object):
    def setupUi(self, ChildWindow):
        ChildWindow.setObjectName("ChildWindow")
        ChildWindow.resize(730, 600)
        ChildWindow.setMinimumSize(QtCore.QSize(730, 600))
        ChildWindow.setMaximumSize(QtCore.QSize(730, 600))
        self.centralwidget = QtWidgets.QWidget(ChildWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lbl_child_title = QtWidgets.QLabel(self.centralwidget)
        self.lbl_child_title.setGeometry(QtCore.QRect(0, 0, 730, 25))
        self.lbl_child_title.setMinimumSize(QtCore.QSize(730, 25))
        self.lbl_child_title.setMaximumSize(QtCore.QSize(730, 25))
        font = QtGui.QFont()
        font.setFamily("Verdana")
        font.setPointSize(13)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.lbl_child_title.setFont(font)
        self.lbl_child_title.setAutoFillBackground(False)
        self.lbl_child_title.setStyleSheet("background-color: rgb(11, 91, 172);\n"
                                          "font: 13pt \"Verdana\";\n"
                                          "color: white;")
        self.lbl_child_title.setFrameShape(QtWidgets.QFrame.Shape.Box)
        self.lbl_child_title.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.lbl_child_title.setLineWidth(2)
        self.lbl_child_title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
        self.lbl_child_title.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
        self.lbl_child_title.setObjectName("lbl_child_title")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(82, 40, 567, 180))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.grd_runactions = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.grd_runactions.setContentsMargins(0, 0, 0, 0)
        self.grd_runactions.setObjectName("grd_runactions")
        self.btn_runprechecks = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.btn_runprechecks.setObjectName("btn_runprechecks")
        self.grd_runactions.addWidget(self.btn_runprechecks, 0, 1, 1, 1)
        self.lbl_precheckstatus = QtWidgets.QLabel(self.gridLayoutWidget)
        self.lbl_precheckstatus.setObjectName("lbl_precheckstatus")
        self.grd_runactions.addWidget(self.lbl_precheckstatus, 1, 1, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
        self.grd_runactions.addItem(spacerItem, 2, 1, 1, 1)
        self.btn_runmanual = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.btn_runmanual.setObjectName("btn_runmanual")
        self.grd_runactions.addWidget(self.btn_runmanual, 4, 2, 1, 1)
        self.btn_dryrun = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.btn_dryrun.setEnabled(False)
        self.btn_dryrun.setObjectName("btn_dryrun")
        self.grd_runactions.addWidget(self.btn_dryrun, 3, 0, 1, 1)
        self.btn_runauto = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.btn_runauto.setEnabled(False)
        self.btn_runauto.setObjectName("btn_runauto")
        self.grd_runactions.addWidget(self.btn_runauto, 4, 0, 1, 1)
        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 230, 711, 341))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.lbl_actionlog = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.lbl_actionlog.setAlignment(QtCore.Qt.AlignmentFlag.AlignBottom|QtCore.Qt.AlignmentFlag.AlignHCenter)
        self.lbl_actionlog.setObjectName("lbl_actionlog")
        self.verticalLayout.addWidget(self.lbl_actionlog)
        self.txt_actionlog = QtWidgets.QTextEdit(self.verticalLayoutWidget)
        self.txt_actionlog.setEnabled(True)
        self.txt_actionlog.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.TextSelectableByKeyboard|QtCore.Qt.TextInteractionFlag.TextSelectableByMouse)
        self.txt_actionlog.setObjectName("txt_actionlog")
        self.verticalLayout.addWidget(self.txt_actionlog)
        ChildWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(ChildWindow)
        self.statusbar.setObjectName("statusbar")
        ChildWindow.setStatusBar(self.statusbar)

        self.retranslateUi(ChildWindow)
        QtCore.QMetaObject.connectSlotsByName(ChildWindow)

    def retranslateUi(self, ChildWindow):
        _translate = QtCore.QCoreApplication.translate
        ChildWindow.setWindowTitle(_translate("ChildWindow", "I am the child window of the parent window"))
        self.lbl_child_title.setText(_translate("ChildWindow", "Child Window"))
        self.btn_runprechecks.setText(_translate("ChildWindow", "Run Pre-Checks"))
        self.lbl_precheckstatus.setText(_translate("ChildWindow", "Precheck Status: Not Run"))
        self.btn_runmanual.setText(_translate("ChildWindow", "Run Manual"))
        self.btn_dryrun.setText(_translate("ChildWindow", "Perform Dry Run"))
        self.btn_runauto.setText(_translate("ChildWindow", "Run Auto"))
        self.lbl_actionlog.setText(_translate("ChildWindow", "Action Log"))

    ...
    button click functions, etc
    ...


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainwindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(mainwindow)
    mainwindow.show()
    sys.exit(app.exec())
  • Does this answer your question? [PyQt window closes immediately after opening](https://stackoverflow.com/questions/16069713/pyqt-window-closes-immediately-after-opening) – musicamante Jul 13 '22 at 18:16
  • Also read [this](https://stackoverflow.com/q/61123846) related post. You have to keep a reference of the window, otherwise it will be garbage collected and immediately destroyed as soon as the `openchild_button_clicked` function returns. Finally, do *not* edit pyuic files, as it's considered bad practice (and is also discouraged, as the warning on top of those files points out), instead follow the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). – musicamante Jul 13 '22 at 18:17
  • @musicamante you were on the right track there, it was apparently being destroyed faster than you can see on screen. I moved `child_window = QtWidgets.QMainWindow() child_ui = Ui_ChildWindow()` out from the button action function to the setupUi of the parent window and added `self.` to the variables and that resolved my problem. Thank you so much! – numbers1thru9 Jul 13 '22 at 18:53
  • Also, I wasnt editing the python generated files, just combining them into a single file with the classes, without modifying them – numbers1thru9 Jul 13 '22 at 18:58
  • That's irrelevant, you should *not* directly use the code of those files, not even to "merge" them, otherwise as soon as you may need to change something in the UI you'll certainly have some problems (and possibly massive headaches) to merge back the new generated code and the existing one. Those files should *always* be left as they are, without caring about their actual contents. As said, please follow the guidelines above. Also, please accept the duplicate proposal. – musicamante Jul 13 '22 at 19:22

0 Answers0