单击时无法关闭窗口

Posted

技术标签:

【中文标题】单击时无法关闭窗口【英文标题】:cant able to close the window when clicked 【发布时间】:2017-06-25 12:32:03 【问题描述】:

在 PyQt4 应用程序中单击“确定”按钮时,我无法关闭窗口,我尝试了很多但没有任何效果,我将我的代码发布在下面以供您参考。 请帮帮我...谢谢

在 PyQt4 应用程序中单击“确定”按钮时,我无法关闭窗口,我尝试了很多但没有任何效果,我将我的代码发布在下面以供您参考。 请帮帮我...谢谢

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GUIAPP.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDialog

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 60, 281, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        font.setBold(False)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.label.setIndent(0)
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(80, 170, 81, 21))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 170, 81, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(self.closeEvent)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "DO YOU WANT TO CONTINUE", None))
        self.pushButton.setText(_translate("Dialog", "ok", None))
        self.pushButton_2.setText(_translate("Dialog", "cancel", None))

    def closeEvent(self):
        self.close()
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

问题是 Ui_Dialog 不是一个小部件,它是一个填充图形部分的类。您应该通过Dialog.close() 关闭窗口,因为对话框是一个小部件。为此,您可以使用以下内容:

self.pushButton_2.clicked.connect(Dialog.close)

完整代码:

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDialog

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 60, 281, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        font.setBold(False)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.label.setIndent(0)
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(80, 170, 81, 21))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 170, 81, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(Dialog.close)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "DO YOU WANT TO CONTINUE", None))
        self.pushButton.setText(_translate("Dialog", "ok", None))
        self.pushButton_2.setText(_translate("Dialog", "cancel", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

另外一种推荐的方式是,除了使用Ui_Dialog作为接口之外,实现逻辑的类并继承自QDialog。

class Dialog(QtGui.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent=parent)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(dialog.close)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    dialog = Dialog() 
    sys.exit(app.exec_())

完整代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'GUIAPP.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDialog

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(400, 300)
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(60, 60, 281, 41))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Times New Roman"))
        font.setPointSize(14)
        font.setBold(False)
        font.setWeight(50)
        self.label.setFont(font)
        self.label.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.label.setIndent(0)
        self.label.setObjectName(_fromUtf8("label"))
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(80, 170, 81, 21))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 170, 81, 23))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.label.setText(_translate("Dialog", "DO YOU WANT TO CONTINUE", None))
        self.pushButton.setText(_translate("Dialog", "ok", None))
        self.pushButton_2.setText(_translate("Dialog", "cancel", None))

class Dialog(QtGui.QDialog, Ui_Dialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent=parent)
        self.setupUi(self)
        self.pushButton_2.clicked.connect(dialog.close)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    dialog = Dialog() 
    sys.exit(app.exec_())

注意:closeEvent 是在窗口关闭之前运行的方法,在关闭之前分析是否需要覆盖它。

【讨论】:

以上是关于单击时无法关闭窗口的主要内容,如果未能解决你的问题,请参考以下文章

在 ExtJS 4.1 中单击外部窗口时如何关闭窗口?

单击外部组件时关闭弹出窗口

无法关闭窗口 - 为啥?

关闭窗口后页面无法点击

在窗口外单击时如何关闭/隐藏窗口?

单击窗口外覆盖时如何关闭 Kendo UI 模态窗口?