pyqt6實(shí)現(xiàn)關(guān)閉窗口前彈出確認(rèn)框的示例代碼
功能描述
關(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ù)說(shuō)明:
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)選中的按鈕(來(lái)自buttons之一)
返回值為選中的按鈕(來(lái)自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.StandardButtonQWidget.closeEvent方法
QWidget.closeEvent() 是一個(gè)在窗口關(guān)閉時(shí)自動(dòng)調(diào)用的函數(shù),用于處理窗口關(guān)閉事件。當(dāng)用戶點(diǎn)擊窗口的關(guān)閉按鈕或使用操作系統(tǒng)提供的快捷鍵來(lái)關(guān)閉窗口時(shí),該函數(shù)就會(huì)被觸發(fā)。
函數(shù)原型如下:
def closeEvent(self, event):
"""
處理窗口關(guān)閉事件。
參數(shù):
event (QCloseEvent) -- 關(guān)閉事件對(duì)象,包含了與關(guān)閉事件相關(guān)的信息。
返回值:
無(wú)返回值。如果需要阻止窗口關(guān)閉,可以返回 True;否則返回 False。
"""
在 closeEvent() 函數(shù)中,我們可以編寫自定義的代碼來(lái)處理窗口關(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)文章
DJANGO-ALLAUTH社交用戶系統(tǒng)的安裝配置
django-allauth是集成了local用戶系統(tǒng)和social用戶系統(tǒng),其social用戶系統(tǒng)可以掛載多個(gè)賬戶。也是一個(gè)流行度非常高的Django user系統(tǒng),我們這里簡(jiǎn)單介紹下,分享下個(gè)人的使用經(jīng)驗(yàn)2014-11-11
Python的Flask路由實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了Python的Flask路由實(shí)現(xiàn)實(shí)例代碼,在啟動(dòng)程序時(shí),python解釋器會(huì)從上到下對(duì)代碼進(jìn)行解釋,當(dāng)遇到裝飾器時(shí),會(huì)執(zhí)行,并把函數(shù)對(duì)應(yīng)的路由以字典的形式進(jìn)行存儲(chǔ),當(dāng)請(qǐng)求到來(lái)時(shí),即可根據(jù)路由查找對(duì)應(yīng)要執(zhí)行的函數(shù)方法,需要的朋友可以參考下2023-08-08
老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)
下面小編就為大家?guī)?lái)一篇老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
pycharm出現(xiàn)No?pyvenv.cfg?file錯(cuò)誤的問題解決
本文主要介紹了pycharm出現(xiàn)No?pyvenv.cfg?file錯(cuò)誤的問題解決,主要是通過(guò)恢復(fù)歷史記錄中的未刪除狀態(tài)來(lái)解決,下面就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下2025-05-05
Python基礎(chǔ)之輸入input與輸出print函數(shù)詳解
這篇文章主要帶大家深入理解輸入input與輸出print函數(shù)詳解的核心概念與實(shí)踐方法,掌握關(guān)鍵技術(shù)要點(diǎn),了解實(shí)際應(yīng)用場(chǎng)景與最佳實(shí)踐,需要的朋友可以參考下2026-04-04
python使用wmi模塊獲取windows下硬盤信息的方法
這篇文章主要介紹了python使用wmi模塊獲取windows下硬盤信息的方法,涉及Python獲取系統(tǒng)硬件信息的相關(guān)技巧,需要的朋友可以參考下2015-05-05
django admin 自定義替換change頁(yè)面模板的方法
今天小編就為大家分享一篇django admin 自定義替換change頁(yè)面模板的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08

