【问题标题】:How to make pop up window close automatically after main window is closed关闭主窗口后如何使弹出窗口自动关闭
【发布时间】:2017-12-27 05:54:23
【问题描述】:

我正在使用 PyQT 创建 QMainWindow 窗口,该窗口在单击按钮时会打开另一个窗口。我的问题是,即使我关闭了生成它的主窗口,弹出窗口仍然显示。这与发布的here 非常相似,但用 C++ 编写,我只能使用 Python。如何在 Python 中实现答案?这是我的代码:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class Pycryptor:

    def mainGui(self):
        app = QApplication(sys.argv)
        #MainWindow
        self.mainWin = QMainWindow()
        self.mainWin.setGeometry(200,200,500,432)
        self.mainWin.show()
        #MenuBar
        mainMenu = self.mainWin.menuBar()
        mainMenu.setNativeMenuBar(False)
        aboutMenu = mainMenu.addMenu('A&bout')
        helpButton = QAction(QIcon(),'Help',self.mainWin)
        helpButton.triggered.connect(self.helpPopup)
        aboutMenu.addAction(helpButton)
        sys.exit(app.exec_())

    def helpPopup(self):
        self.popup = HelpWindow()
        self.popup.setGeometry(800,200,300,500)
        self.popup.show()

class HelpWindow(QWidget):
    def __init__(self):
        QWidget.__init__(self)

if __name__ == '__main__':
    p = Pycryptor()
    p.mainGui()

【问题讨论】:

  • HelpWindow 对象 setParent MainWindow
  • 对不起,你能告诉我怎么做吗?我刚刚开始学习课程

标签: python-3.x qt pyqt4


【解决方案1】:

在 c++ 中,您可以将您的小部件对象设置为 mainWindow 对象。当父母被摧毁时,他将跟随他所有的孩子。

例如:

void MainWindow::on_pushButton_clicked()
{
    Form *fm = new Form();
    fm->setParent(this);
    fm->setWindowFlags(Qt::Window);
    fm->show();
}

我不知道如何创建这个 pyqt,但您可以阅读文档: http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setParent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多