4

我正在尝试遵循教程,但是它是在我使用 PyQt5 时为 PyQt4 制作的。

我有一个QPushButton名为的对象btn,并希望它的clicked()信号触发QApplication closeAllWindows()插槽。我已经阅读了文档,但似乎没有帮助,我是 Python 新手。

有人知道怎么做这个吗?

4

1 回答 1

1

签出这个例子:

#!/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_())
于 2013-08-27T18:09:53.633 回答