简体   繁体   English

Pyside2第二个窗口(QDialog)关闭主窗口

[英]Pyside2 second window(QDialog) closes the main one

import sys

from PySide2.QtCore import QFile
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader


class MyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        loader = QUiLoader()
        self.ui = loader.load("mainWindow.ui", self)

        self.ui.pushButton_call_dialog.clicked.connect(self.call_dialog)
        self.ui.close()

        self.ui.show()


    def call_dialog(self):
        loader = QUiLoader()
        self.dialog = loader.load("dialog.ui")

        self.dialog.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    window = MyMainWindow()
    window.show
    sys.exit(app.exec_())

Hi everyone, any idea why the second (dialog) window closes the entire application?大家好,知道为什么第二个(对话框)窗口会关闭整个应用程序吗? Of course, it is not a crash since i'm getting a message saying:当然,这不是崩溃,因为我收到一条消息:

Process finished with exit code 0进程以退出代码 0 结束

Thanks for your help谢谢你的帮助

You could handle your QDialog on a separate class, and then make them interact only, the structure might change a bit, but you can see if it's a viable answer:你可以在一个单独的类上处理你的 QDialog,然后让它们只交互,结构可能会改变一点,但你可以看看它是否是一个可行的答案:

import sys
from PySide2.QtWidgets import *

class MyWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        button = QPushButton("Dialog")
        button.clicked.connect(self.open_dialog)
        self.setCentralWidget(button)

    def open_dialog(self):
        dialog = MyDialog()
        dialog.show()
        dialog.exec_()


class MyDialog(QDialog):
    def __init__(self):
        QDialog.__init__(self)

        button = QPushButton("Close")
        button.clicked.connect(self.close_dialog)

        layout = QHBoxLayout()
        layout.addWidget(button)
        self.setLayout(layout)

    def close_dialog(self):
        self.close()

if __name__ == "__main__":
    app = QApplication()
    m = MyWindow()
    m.show()
    sys.exit(app.exec_())

Just notice that you should include the setUp step on each class.请注意,您应该在每个类中包含setUp步骤。 Hope it helps.希望能帮助到你。

To put the dialog into a separate class didn't work for either.将对话框放到一个单独的类中也不起作用。 Every time the Dialog.close() event was called, it closes the whole application.每次调用 Dialog.close() 事件时,它都会关闭整个应用程序。

What worked for me, was to use hide() instead对我有用的是使用 hide() 代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM