凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

3. PyQt6 退出事件

2023-10-17 python 200
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt6.QtGui import QGuiApplication


class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("PyQt6 窗口案例")

        self.setGeometry(100, 100, 400, 300)
        self.center()

    def closeEvent(self, event):
        reply = QMessageBox.question(self, "消息", "确定要退出?",
                                     QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
                                     QMessageBox.StandardButton.No)
        if reply == QMessageBox.StandardButton.Yes:
            event.accept()
        else:
            event.ignore()

    def center(self):
        qr = self.frameGeometry()
        cp = QGuiApplication.primaryScreen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


文章评论

0条评论