Python PyQt5-如何将关闭键(红色x)连接到函数
我正在尝试将Python PyQt5-如何将关闭键(红色x)连接到函数,python,pyqt,qt5,pyqt5,Python,Pyqt,Qt5,Pyqt5,我正在尝试将关闭键(红色x)连接到功能(消息框)。到目前为止我所尝试的都不起作用;单击关闭键(红色x)时,我的代码将冻结。没有错误消息 这是我的密码: from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QMessageBox class Ui_MainWindow(QMainWindow): def setupUi(self, MainWindow): Mai
关闭键(红色x)
连接到功能(消息框)。到目前为止我所尝试的都不起作用;单击关闭键(红色x)
时,我的代码将冻结。没有错误消息
这是我的密码:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class Ui_MainWindow(QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setWindowModality(QtCore.Qt.NonModal)
MainWindow.resize(987, 746)
MainWindow.setMinimumSize(567, 456)
MainWindow.setMaximumSize(987, 746)
font = QtGui.QFont()
font.setPointSize(9)
MainWindow.setFont(font)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", ""))
def Exit(self):
reply = QMessageBox.question(self, 'Quit', 'Are You Sure to Quit?', QMessageBox.No| QMessageBox.Yes)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(Ui_MainWindow.Exit)
MainWindow = QtWidgets.QMainWindow()
MainWindow.show()
Ui = Ui_MainWindow()
Ui.setupUi(MainWindow)
sys.exit(app.exec_())
谢谢。我不明白您为什么要创建一个类Ui\u主窗口,但下面是一个示例,说明我将如何执行您尝试执行的操作:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setObjectName("MainWindow")
self.setWindowModality(QtCore.Qt.NonModal)
self.resize(987, 746)
self.setMinimumSize(567, 456)
self.setMaximumSize(987, 746)
font = QtGui.QFont()
font.setPointSize(9)
self.setFont(font)
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Quit', 'Are You Sure to Quit?', QMessageBox.No | QMessageBox.Yes)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
我不明白您为什么要创建一个类Ui_主窗口,但下面是一个示例,说明我将如何执行您尝试执行的操作:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setupUI()
def setupUI(self):
self.setObjectName("MainWindow")
self.setWindowModality(QtCore.Qt.NonModal)
self.resize(987, 746)
self.setMinimumSize(567, 456)
self.setMaximumSize(987, 746)
font = QtGui.QFont()
font.setPointSize(9)
self.setFont(font)
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Quit', 'Are You Sure to Quit?', QMessageBox.No | QMessageBox.Yes)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
非常感谢你的回答。我的代码是由Qt设计器生成的。这就是出现
Ui\u主窗口的原因。我试着用super()。\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuu()
为类插入\uuuuuu init\uuuuuuuuuuuuuu>构造函数,但它并没有完全解决我的问题。我的代码现在没有冻结,但它仍然没有连接到消息框,它只是退出。很简单,有两件事:-你的类中必须有closeEvent函数-第二件事,显示的窗口是MainWindow,但您试图处理的类是Ui_MainWindow。谢谢您的回复。我无法将您的答案应用于我的代码,它似乎不起作用。你能在我的原始帖子中展示如何在不添加def\uuu init\uuuu(self):
的情况下将你的解决方案应用到我的实际代码中吗?如果你在这里展示的代码是你所有的,那么你可以复制粘贴我的代码。很明显,我发布的代码并不是我所有的。正如你所看到的,这是一扇空窗户。显然,没有人会只为空窗口编写代码。我所发布的是,我希望是,一个最小的,完整的和可验证的例子。非常感谢你的回答。我的代码是由Qt设计器生成的。这就是出现Ui\u主窗口的原因。我试着用super()。\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuu()
为类插入\uuuuuu init\uuuuuuuuuuuuuu>构造函数,但它并没有完全解决我的问题。我的代码现在没有冻结,但它仍然没有连接到消息框,它只是退出。很简单,有两件事:-你的类中必须有closeEvent函数-第二件事,显示的窗口是MainWindow,但您试图处理的类是Ui_MainWindow。谢谢您的回复。我无法将您的答案应用于我的代码,它似乎不起作用。你能在我的原始帖子中展示如何在不添加def\uuu init\uuuu(self):
的情况下将你的解决方案应用到我的实际代码中吗?如果你在这里展示的代码是你所有的,那么你可以复制粘贴我的代码。很明显,我发布的代码并不是我所有的。正如你所看到的,这是一扇空窗户。显然,没有人会只为空窗口编写代码。我所张贴的是,我希望是,一个最小的,完整的和可验证的例子。