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

 更新时间:2024年02月19日 11:41:48   作者:老狼IT工作室  
本文主要介绍了pyqt6实现关闭窗口前弹出确认框的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

功能描述

关闭右上角的关闭(×)按钮时,弹出确认框,选择“是(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())

 运行效果

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

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

到此这篇关于pyqt6实现关闭窗口前弹出确认框的示例代码的文章就介绍到这了,更多相关pyqt6  弹出确认框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python实现PDF与SVG互转

    使用Python实现PDF与SVG互转

    SVG(可缩放矢量图形)和PDF(便携式文档格式)是两种常见且广泛使用的文件格式,本文将详细介绍如何使用 Python 实现 SVG 和 PDF 之间的相互转换,感兴趣的可以了解下
    2025-02-02
  • Python学习笔记之图片人脸检测识别实例教程

    Python学习笔记之图片人脸检测识别实例教程

    这篇文章主要给大家介绍了关于Python学习笔记之图片人脸检测识别的相关资料,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • python 集合 并集、交集 Series list set 转换的实例

    python 集合 并集、交集 Series list set 转换的实例

    今天小编就为大家分享一篇python 集合 并集、交集 Series list set 转换的实例。具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • selenium获取当前页面的url、源码、title的方法

    selenium获取当前页面的url、源码、title的方法

    这篇文章主要介绍了selenium获取当前页面的url、源码、title的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Python的批量远程管理和部署工具Fabric用法实例

    Python的批量远程管理和部署工具Fabric用法实例

    这篇文章主要介绍了Python的批量远程管理和部署工具Fabric用法,实例分析了Fabric的功能与具体使用方法,需要的朋友可以参考下
    2015-01-01
  • Python OpenCV实现图像增强操作详解

    Python OpenCV实现图像增强操作详解

    由于很多不确定因素,导致图像采集的光环境极其复杂;为了提高目标检测模型的泛化能力,本文将使用python中的opencv模块实现常见的图像增强方法,感兴趣的可以了解一下
    2022-10-10
  • django自带调试服务器的使用详解

    django自带调试服务器的使用详解

    今天小编就为大家分享一篇django自带调试服务器的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python中list列表添加元素的3种方法总结

    Python中list列表添加元素的3种方法总结

    这篇文章主要介绍了Python中list列表添加元素的3种方法总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • django基于cors解决跨域请求问题详解

    django基于cors解决跨域请求问题详解

    这篇文章主要介绍了django基于cors解决跨域请求问题详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • python将字符串list写入excel和txt的实例

    python将字符串list写入excel和txt的实例

    今天小编就为大家分享一篇python将字符串list写入excel和txt的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07

最新评论