pyqt6實現(xiàn)關(guān)閉窗口前彈出確認框的示例代碼
功能描述
關(guān)閉右上角的關(guān)閉(×)按鈕時,彈出確認框,選擇“是(Yes)”則直接退出窗口,選擇“(否)No”則忽視當前操作,保留窗口處于激活狀態(tài)。
知識點
QMessageBox.question方法
QMessageBox.question() 方法是 PyQt 中用于顯示一個帶有確定和取消按鈕的對話框,并等待用戶點擊其中一個按鈕后返回結(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:父窗口對象,即該對話框的父級窗口。如果為 None,則對話框沒有父級窗口。title:對話框的標題。text:對話框中要顯示的文本內(nèi)容。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.StandardButtonQWidget.closeEvent方法
QWidget.closeEvent() 是一個在窗口關(guān)閉時自動調(diào)用的函數(shù),用于處理窗口關(guān)閉事件。當用戶點擊窗口的關(guān)閉按鈕或使用操作系統(tǒng)提供的快捷鍵來關(guān)閉窗口時,該函數(shù)就會被觸發(fā)。
函數(shù)原型如下:
def closeEvent(self, event):
"""
處理窗口關(guān)閉事件。
參數(shù):
event (QCloseEvent) -- 關(guān)閉事件對象,包含了與關(guān)閉事件相關(guān)的信息。
返回值:
無返回值。如果需要阻止窗口關(guān)閉,可以返回 True;否則返回 False。
"""
在 closeEvent() 函數(shù)中,我們可以編寫自定義的代碼來處理窗口關(guān)閉事件。例如,我們可以在函數(shù)中彈出一個確認對話框,詢問用戶是否真的要關(guān)閉窗口。如果用戶選擇“是”,則允許窗口關(guān)閉;如果用戶選擇“否”,則取消關(guān)閉操作。
實現(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)閉窗口測試')
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())
運行效果

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

如果選擇 No,窗口不關(guān)閉。如果選擇 “Yes”,則窗口關(guān)閉。
到此這篇關(guān)于pyqt6實現(xiàn)關(guān)閉窗口前彈出確認框的示例代碼的文章就介紹到這了,更多相關(guān)pyqt6 彈出確認框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DJANGO-ALLAUTH社交用戶系統(tǒng)的安裝配置
django-allauth是集成了local用戶系統(tǒng)和social用戶系統(tǒng),其social用戶系統(tǒng)可以掛載多個賬戶。也是一個流行度非常高的Django user系統(tǒng),我們這里簡單介紹下,分享下個人的使用經(jīng)驗2014-11-11
老生常談python函數(shù)參數(shù)的區(qū)別(必看篇)
下面小編就為大家?guī)硪黄仙U刾ython函數(shù)參數(shù)的區(qū)別(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
pycharm出現(xiàn)No?pyvenv.cfg?file錯誤的問題解決
本文主要介紹了pycharm出現(xiàn)No?pyvenv.cfg?file錯誤的問題解決,主要是通過恢復歷史記錄中的未刪除狀態(tài)來解決,下面就來詳細的介紹一下,感興趣的可以了解一下2025-05-05
Python基礎之輸入input與輸出print函數(shù)詳解
這篇文章主要帶大家深入理解輸入input與輸出print函數(shù)詳解的核心概念與實踐方法,掌握關(guān)鍵技術(shù)要點,了解實際應用場景與最佳實踐,需要的朋友可以參考下2026-04-04
python使用wmi模塊獲取windows下硬盤信息的方法
這篇文章主要介紹了python使用wmi模塊獲取windows下硬盤信息的方法,涉及Python獲取系統(tǒng)硬件信息的相關(guān)技巧,需要的朋友可以參考下2015-05-05
django admin 自定義替換change頁面模板的方法
今天小編就為大家分享一篇django admin 自定義替換change頁面模板的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

