无法关闭 GUI 应用程序
Unable to close the GUI application
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(277, 244)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.actionQuit.setShortcut("Ctrl+Q")
self.actionQuit.setStatusTip("Quit the application")
self.actionQuit.triggered.connect(MainWindow.closeEvent)
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
class MyWindow(QtWidgets.QMainWindow):
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = MyWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
sys.exit(app.exec_())
当我尝试使用 Ctrl+Q 快捷键关闭应用程序时,会弹出消息框询问问题,当我单击任一选项时,我得到
AttributeError: 'bool' object has no attribute 'ignore'
我哪里错了?
通过像那样将动作连接到函数,self.actionQuit.triggered.connect(MainWindow.closeEvent)
你确保 triggered
信号被传递给函数,但是这个信号是一个布尔值,如 documentation 中所写,这意味着 event
在你的代码中是一个布尔值下面:
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
布尔值没有 ignore
或 accept
属性,因此出现 AttributeError。我没有解决方案,但这是您问题的根源。希望使用键盘快捷键退出程序是一个已经回答的问题。
您不应该直接调用 closeEvent()
方法(或 xxxEvent
的方法),因为这是由 Qt 的内部元素通过事件循环调用的,而您应该使用 close()
方法间接调用 closeEvent()
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(277, 244)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.actionQuit.setShortcut("Ctrl+Q")
self.actionQuit.setStatusTip("Quit the application")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.actionQuit.triggered.connect(self.close)
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(277, 244)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.actionQuit.setShortcut("Ctrl+Q")
self.actionQuit.setStatusTip("Quit the application")
self.actionQuit.triggered.connect(MainWindow.closeEvent)
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
class MyWindow(QtWidgets.QMainWindow):
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = MyWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
sys.exit(app.exec_())
当我尝试使用 Ctrl+Q 快捷键关闭应用程序时,会弹出消息框询问问题,当我单击任一选项时,我得到
AttributeError: 'bool' object has no attribute 'ignore'
我哪里错了?
通过像那样将动作连接到函数,self.actionQuit.triggered.connect(MainWindow.closeEvent)
你确保 triggered
信号被传递给函数,但是这个信号是一个布尔值,如 documentation 中所写,这意味着 event
在你的代码中是一个布尔值下面:
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
布尔值没有 ignore
或 accept
属性,因此出现 AttributeError。我没有解决方案,但这是您问题的根源。希望使用键盘快捷键退出程序是一个已经回答的问题。
您不应该直接调用 closeEvent()
方法(或 xxxEvent
的方法),因为这是由 Qt 的内部元素通过事件循环调用的,而您应该使用 close()
方法间接调用 closeEvent()
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(277, 244)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.actionQuit = QtWidgets.QAction(MainWindow)
self.actionQuit.setObjectName("actionQuit")
self.actionQuit.setShortcut("Ctrl+Q")
self.actionQuit.setStatusTip("Quit the application")
self.menuFile.addAction(self.actionQuit)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionQuit.setText(_translate("MainWindow", "Quit"))
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.actionQuit.triggered.connect(self.close)
def closeEvent(self, event):
print(event)
result = QtWidgets.QMessageBox.question(self,
"Confirm Exit...",
"Are you sure you want to exit ?",
QtWidgets.QMessageBox.Yes| QtWidgets.QMessageBox.No)
event.ignore()
print(result)
if result == QtWidgets.QMessageBox.Yes:
event.accept()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())