pyqt6实现关闭窗口前弹出确认框的示例代码

来自:网络
时间:2024-03-18
阅读:

功能描述

关闭右上角的关闭(×)按钮时,弹出确认框,选择“是(Yes)”则直接退出窗口,选择“(否)No”则忽视当前操作,保留窗口处于激活状态。

知识点

QMessageBox.question方法

QMessageBox.question() 方法是 PyQt 中用于显示一个带有确定和取消按钮的对话框,并等待用户点击其中一个按钮后返回结果的方法。

函数原型:

QMessageBox.question(parent: Optional[QWidget], 
	title: Optional[str], 
	text: Optional[str], 
	buttons: QMessageBox.StandardButton = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), 
	defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton) 
-> QMessageBox.StandardButton

参数说明:

  • parent:父窗口对象,即该对话框的父级窗口。如果为 None,则对话框没有父级窗口。
  • title:对话框的标题。
  • text:对话框中要显示的文本内容。
  • buttons:对话框中要显示的按钮类型,可以是以下值的组合:
    • Yes和No
    • Yes和Cancel
  • defaultButton:对话框中默认选中的按钮(来自buttons之一)

返回值为选中的按钮(来自buttons之一)

class StandardButton(enum.IntFlag):
    NoButton = ... # type: QMessageBox.StandardButton
    Ok = ... # type: QMessageBox.StandardButton
    Save = ... # type: QMessageBox.StandardButton
    SaveAll = ... # type: QMessageBox.StandardButton
    Open = ... # type: QMessageBox.StandardButton
    Yes = ... # type: QMessageBox.StandardButton
    YesToAll = ... # type: QMessageBox.StandardButton
    No = ... # type: QMessageBox.StandardButton
    NoToAll = ... # type: QMessageBox.StandardButton
    Abort = ... # type: QMessageBox.StandardButton
    Retry = ... # type: QMessageBox.StandardButton
    Ignore = ... # type: QMessageBox.StandardButton
    Close = ... # type: QMessageBox.StandardButton
    Cancel = ... # type: QMessageBox.StandardButton
    Discard = ... # type: QMessageBox.StandardButton
    Help = ... # type: QMessageBox.StandardButton
    Apply = ... # type: QMessageBox.StandardButton
    Reset = ... # type: QMessageBox.StandardButton
    RestoreDefaults = ... # type: QMessageBox.StandardButton
    FirstButton = ... # type: QMessageBox.StandardButton
    LastButton = ... # type: QMessageBox.StandardButton
    YesAll = ... # type: QMessageBox.StandardButton
    NoAll = ... # type: QMessageBox.StandardButton
    Default = ... # type: QMessageBox.StandardButton
    Escape = ... # type: QMessageBox.StandardButton
    FlagMask = ... # type: QMessageBox.StandardButton
    ButtonMask = ... # type: QMessageBox.StandardButton

QWidget.closeEvent方法

QWidget.closeEvent() 是一个在窗口关闭时自动调用的函数,用于处理窗口关闭事件。当用户点击窗口的关闭按钮或使用操作系统提供的快捷键来关闭窗口时,该函数就会被触发。

函数原型如下:

def closeEvent(self, event):
    """
    处理窗口关闭事件。

    参数:
        event (QCloseEvent) -- 关闭事件对象,包含了与关闭事件相关的信息。

    返回值:
        无返回值。如果需要阻止窗口关闭,可以返回 True;否则返回 False。
    """

在 closeEvent() 函数中,我们可以编写自定义的代码来处理窗口关闭事件。例如,我们可以在函数中弹出一个确认对话框,询问用户是否真的要关闭窗口。如果用户选择“是”,则允许窗口关闭;如果用户选择“否”,则取消关闭操作。

实现代码

import sys

from PyQt6.QtGui import QCloseEvent
from PyQt6.QtWidgets import QApplication, QWidget, QMessageBox


class ConfirmQuitWindow(QWidget):
    def __init__(self, is_confirm_quit: bool = True):
        super(ConfirmQuitWindow, self).__init__()
        self.is_confirm_quit = is_confirm_quit
        self.setGeometry(0, 0, 500, 300)
        self.setWindowTitle('提示是否关闭窗口测试')

    def closeEvent(self, event: QCloseEvent) -> None:
        if self.is_confirm_quit:
            reply = QMessageBox.question(self, '关闭窗口', '确定退出吗?',
                                         QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
                                         QMessageBox.StandardButton.No)
            if reply == QMessageBox.StandardButton.Yes:
                event.accept()
            else:
                event.ignore()
        else:
            event.accept()


app = QApplication(sys.argv)

window = ConfirmQuitWindow(is_confirm_quit=True)
window.show()

sys.exit(app.exec())

 运行效果

pyqt6实现关闭窗口前弹出确认框的示例代码

点击右上角关闭(x)按钮:

pyqt6实现关闭窗口前弹出确认框的示例代码

如果选择 No,窗口不关闭。如果选择 “Yes”,则窗口关闭。

返回顶部
顶部