K
K
kramick2021-08-11 10:39:14
PyQt
kramick, 2021-08-11 10:39:14

Why is the second PyQT window closing?

Created the main window. I added a menu on top and for one of the QAction (one of the items) I added an action , but the second window does not open ... More precisely, it opens, but closes very quickly, sometimes it is noticeable . How can I solve the problem?

from PyQt5 import QtCore, QtGui, QtWidgets
from Information.GUI.gui_info import Ui_About
# куча кода основного класса
# куча кода основного класса
# куча кода основного класса

class Information(QtWidgets.QMainWindow, Ui_About):
    """Класс со вторым окном"""
    def __init__(self):
        super().__init__()
        self.setupUi(self)

class Main(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.about_act.triggered.connect(self.onClicked)

    def onClicked(self):    # сама функция для показа
        about = Information()
        about.show()

def start_app(name_class):
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = name_class()
    window.show()
    app.exec_()


if __name__ == '__main__':
    start_app(Main)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2021-08-11
@kramick

def onClicked(self):    # сама функция для показа
        about = Information()
        about.show()

After the function terminates, all local variables are destroyed. Store a reference to the new window in the main class
class Information(QtWidgets.QMainWindow):  # не надо наследоваться от двух классов сразу
    """Класс со вторым окном"""

    def __init__(self):
        super().__init__()
        self.ui = Ui_About()
        self.ui.setupUi(self)

        self.information_window = Information() # если передать родителя, и установить Modality = True, окно будет модальным

    def onClicked(self):    # сама функция для показа
        self.information_window.show()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question