欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼

 更新時(shí)間:2024年02月19日 11:41:48   作者:老狼IT工作室  
本文主要介紹了pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

功能描述

關(guān)閉右上角的關(guān)閉(×)按鈕時(shí),彈出確認(rèn)框,選擇“是(Yes)”則直接退出窗口,選擇“(否)No”則忽視當(dāng)前操作,保留窗口處于激活狀態(tài)。

知識(shí)點(diǎn)

QMessageBox.question方法

QMessageBox.question() 方法是 PyQt 中用于顯示一個(gè)帶有確定和取消按鈕的對(duì)話框,并等待用戶點(diǎn)擊其中一個(gè)按鈕后返回結(jié)果的方法。

函數(shù)原型:

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

參數(shù)說明:

  • parent:父窗口對(duì)象,即該對(duì)話框的父級(jí)窗口。如果為 None,則對(duì)話框沒有父級(jí)窗口。
  • title:對(duì)話框的標(biāo)題。
  • text:對(duì)話框中要顯示的文本內(nèi)容。
  • buttons:對(duì)話框中要顯示的按鈕類型,可以是以下值的組合:
    • Yes和No
    • Yes和Cancel
  • defaultButton:對(duì)話框中默認(rèn)選中的按鈕(來自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() 是一個(gè)在窗口關(guān)閉時(shí)自動(dòng)調(diào)用的函數(shù),用于處理窗口關(guān)閉事件。當(dāng)用戶點(diǎn)擊窗口的關(guān)閉按鈕或使用操作系統(tǒng)提供的快捷鍵來關(guān)閉窗口時(shí),該函數(shù)就會(huì)被觸發(fā)。

函數(shù)原型如下:

def closeEvent(self, event):
    """
    處理窗口關(guān)閉事件。

    參數(shù):
        event (QCloseEvent) -- 關(guān)閉事件對(duì)象,包含了與關(guān)閉事件相關(guān)的信息。

    返回值:
        無返回值。如果需要阻止窗口關(guān)閉,可以返回 True;否則返回 False。
    """

在 closeEvent() 函數(shù)中,我們可以編寫自定義的代碼來處理窗口關(guān)閉事件。例如,我們可以在函數(shù)中彈出一個(gè)確認(rèn)對(duì)話框,詢問用戶是否真的要關(guān)閉窗口。如果用戶選擇“是”,則允許窗口關(guān)閉;如果用戶選擇“否”,則取消關(guān)閉操作。

實(shí)現(xiàn)代碼

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('提示是否關(guān)閉窗口測(cè)試')

    def closeEvent(self, event: QCloseEvent) -> None:
        if self.is_confirm_quit:
            reply = QMessageBox.question(self, '關(guān)閉窗口', '確定退出嗎?',
                                         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())

 運(yùn)行效果

點(diǎn)擊右上角關(guān)閉(x)按鈕:

如果選擇 No,窗口不關(guān)閉。如果選擇 “Yes”,則窗口關(guān)閉。

到此這篇關(guān)于pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼的文章就介紹到這了,更多相關(guān)pyqt6  彈出確認(rèn)框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python實(shí)現(xiàn)PDF與SVG互轉(zhuǎn)

    使用Python實(shí)現(xiàn)PDF與SVG互轉(zhuǎn)

    SVG(可縮放矢量圖形)和PDF(便攜式文檔格式)是兩種常見且廣泛使用的文件格式,本文將詳細(xì)介紹如何使用?Python?實(shí)現(xiàn)?SVG?和?PDF?之間的相互轉(zhuǎn)換,感興趣的可以了解下
    2025-02-02
  • Python學(xué)習(xí)筆記之圖片人臉檢測(cè)識(shí)別實(shí)例教程

    Python學(xué)習(xí)筆記之圖片人臉檢測(cè)識(shí)別實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于Python學(xué)習(xí)筆記之圖片人臉檢測(cè)識(shí)別的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python 集合 并集、交集 Series list set 轉(zhuǎn)換的實(shí)例

    python 集合 并集、交集 Series list set 轉(zhuǎn)換的實(shí)例

    今天小編就為大家分享一篇python 集合 并集、交集 Series list set 轉(zhuǎn)換的實(shí)例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • selenium獲取當(dāng)前頁面的url、源碼、title的方法

    selenium獲取當(dāng)前頁面的url、源碼、title的方法

    這篇文章主要介紹了selenium獲取當(dāng)前頁面的url、源碼、title的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Python的批量遠(yuǎn)程管理和部署工具Fabric用法實(shí)例

    Python的批量遠(yuǎn)程管理和部署工具Fabric用法實(shí)例

    這篇文章主要介紹了Python的批量遠(yuǎn)程管理和部署工具Fabric用法,實(shí)例分析了Fabric的功能與具體使用方法,需要的朋友可以參考下
    2015-01-01
  • Python?OpenCV實(shí)現(xiàn)圖像增強(qiáng)操作詳解

    Python?OpenCV實(shí)現(xiàn)圖像增強(qiáng)操作詳解

    由于很多不確定因素,導(dǎo)致圖像采集的光環(huán)境極其復(fù)雜;為了提高目標(biāo)檢測(cè)模型的泛化能力,本文將使用python中的opencv模塊實(shí)現(xiàn)常見的圖像增強(qiáng)方法,感興趣的可以了解一下
    2022-10-10
  • django自帶調(diào)試服務(wù)器的使用詳解

    django自帶調(diào)試服務(wù)器的使用詳解

    今天小編就為大家分享一篇django自帶調(diào)試服務(wù)器的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python中l(wèi)ist列表添加元素的3種方法總結(jié)

    Python中l(wèi)ist列表添加元素的3種方法總結(jié)

    這篇文章主要介紹了Python中l(wèi)ist列表添加元素的3種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • django基于cors解決跨域請(qǐng)求問題詳解

    django基于cors解決跨域請(qǐng)求問題詳解

    這篇文章主要介紹了django基于cors解決跨域請(qǐng)求問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python將字符串list寫入excel和txt的實(shí)例

    python將字符串list寫入excel和txt的實(shí)例

    今天小編就為大家分享一篇python將字符串list寫入excel和txt的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07

最新評(píng)論