使QPushButton关闭所有打开的程序窗口

时间:2013-08-27 08:08:58

标签: python qt pyqt pyqt5

我正在尝试关注this教程,但是当我使用PyQt5时,它是为PyQt4制作的。

我有一个名为QPushButton的{​​{1}}对象,希望其btn信号触发clicked() QApplication个广告位。我已经阅读了文档,但似乎没有帮助,我是Python的新手。

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:0)

查看此示例:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
import sys

from PyQt4 import QtGui, QtCore

#---------
# DEFINE
#---------
class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonClose = QtGui.QPushButton(self)
        self.pushButtonClose.setText("Close Windows!")
        self.pushButtonClose.clicked.connect(self.on_pushButtonClose_clicked)

        self.pushButtonWindows = QtGui.QPushButton(self)
        self.pushButtonWindows.setText("Create Windows!")
        self.pushButtonWindows.clicked.connect(self.on_pushButtonWindows_clicked)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.pushButtonClose)
        self.layoutVertical.addWidget(self.pushButtonWindows)

    @QtCore.pyqtSlot()
    def on_pushButtonWindows_clicked(self):
        position = self.rect().bottom()

        for dialogNumber in range(3):
            dialog = QtGui.QDialog(self)
            dialog.show()
            dialog.setGeometry(
                position,
                position,
                dialog.width(),
                dialog.height()
            )

            position += 10

    @QtCore.pyqtSlot()
    def on_pushButtonClose_clicked(self):
        app = QtGui.QApplication.instance()
        app.closeAllWindows()

#---------
# MAIN
#---------
if __name__ == "__main__":    
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.setGeometry(0, 0, 333, 111)
    main.setWindowFlags(
        main.windowFlags() |
        QtCore.Qt.WindowStaysOnTopHint |
        QtCore.Qt.X11BypassWindowManagerHint
    )
    main.show()

    sys.exit(app.exec_())