PyQt - QMessageBox



QMessageBox 是一個常用的模態對話方塊,用於顯示一些資訊訊息,並可以選擇性地要求使用者透過點選其上的任何一個標準按鈕來響應。每個標準按鈕都有一個預定義的標題、一個角色,並返回一個預定義的十六進位制數字。

與 QMessageBox 類關聯的重要方法和列舉在以下表格中給出:

序號 方法及描述
1

setIcon()

顯示與訊息嚴重性相對應的預定義圖示

Question 疑問

Information 資訊

Warning 警告

Critical 嚴重錯誤

2

setText()

設定要顯示的主訊息文字

3

setInformativeText()

顯示附加資訊

4

setDetailText()

對話方塊顯示一個“詳細資訊”按鈕。點選該按鈕時顯示此文字

5

setTitle()

顯示對話方塊的自定義標題

6

setStandardButtons()

要顯示的標準按鈕列表。每個按鈕都與以下內容相關聯:

QMessageBox.Ok 0x00000400

QMessageBox.Open 0x00002000

QMessageBox.Save 0x00000800

QMessageBox.Cancel 0x00400000

QMessageBox.Close 0x00200000

QMessageBox.Yes 0x00004000

QMessageBox.No 0x00010000

QMessageBox.Abort 0x00040000

QMessageBox.Retry 0x00080000

QMessageBox.Ignore 0x00100000

7

setDefaultButton()

將按鈕設定為預設按鈕。如果按下 Enter 鍵,它將發出 clicked 訊號

8

setEscapeButton()

設定按鈕,使其在按下 Escape 鍵時被視為已點選

QMessageBox 沒有定義預定義圖示,而是透過樣式提供它們。預設值為無圖示。訊息框在所有情況下都保持相同。建議使用表格中建議的標準圖示或平臺的樣式指南中的標準圖示。

在這種情況下,沒有一個預定義圖示適合我們的訊息框,我們可以透過設定圖示畫素圖屬性而不是圖示屬性來建立自定義圖示。

示例 1

在本例中,說明了使用 QMessageBox 類及其方法的預定義圖示。

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
def window():
   app = QApplication(sys.argv)
   w = QWidget()

   # Create buttons
   b1 = QPushButton(w)
   b1.setText("Information")
   b1.move(45, 50)

   b2 = QPushButton(w)
   b2.setText("Warning")
   b2.move(150, 50)

   b3 = QPushButton(w)
   b3.setText("Question")
   b3.move(50, 150)

   b4 = QPushButton(w)
   b4.setText("Critical")
   b4.move(150, 150)

   # Connect button signals to functions
   b1.clicked.connect(show_info_messagebox)
   b2.clicked.connect(show_warning_messagebox)
   b3.clicked.connect(show_question_messagebox)
   b4.clicked.connect(show_critical_messagebox)

   # Set window title
   w.setWindowTitle("PyQt MessageBox")

   # Show all widgets
   w.show()

   # Start the app
   sys.exit(app.exec())

def show_info_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Information)
   msg.setText("Information")
   msg.setWindowTitle("Information MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_warning_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Warning)
   msg.setText("Warning")
   msg.setWindowTitle("Warning MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_question_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Question)
   msg.setText("Question")
   msg.setWindowTitle("Question MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()
def show_critical_messagebox():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Icon.Critical)
   msg.setText("Critical")
   msg.setWindowTitle("Critical MessageBox")
   msg.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel)
   retval = msg.exec()

if __name__ == "__main__":
   window()

輸出

執行程式碼後,我們看到四個點選按鈕以獲取特定的圖示。

QMessageBox Output example one

示例 2

在以下示例中,點選頂層視窗上的按鈕的訊號,連線的函式將顯示訊息框對話方塊。

msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText("This is a message box")
msg.setInformativeText("This is additional information")
msg.setWindowTitle("MessageBox demo")
msg.setDetailedText("The details are as follows:")

setStandardButton() 函式顯示所需的按鈕。

msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)

buttonClicked() 訊號連線到一個槽函式,該函式識別訊號源的標題。

msg.buttonClicked.connect(msgbtn)

該示例的完整程式碼如下:

import sys
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *

def window():
   app = QApplication(sys.argv)
   w = QWidget()
   b = QPushButton(w)
   b.setText("Show message!")
   
   b.move(100,50)
   b.clicked.connect(showdialog)
   w.setWindowTitle("PyQt MessageBox demo")
   w.show()
   sys.exit(app.exec_())

def showdialog():
   msg = QMessageBox()
   msg.setIcon(QMessageBox.Information)
   
   msg.setText("This is a message box")
   msg.setInformativeText("This is additional information")
   msg.setWindowTitle("MessageBox demo")
   msg.setDetailedText("The details are as follows:")
   msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
   msg.buttonClicked.connect(msgbtn)

   retval = msg.exec_()

def msgbtn(i):
   print ("Button pressed is:",i.text())

if __name__ == '__main__':
   window()

輸出

以上程式碼產生以下輸出。當單擊主視窗按鈕時,將彈出訊息框:

QMessageBox Output

如果單擊訊息框上的“確定”或“取消”按鈕,控制檯將輸出以下內容:

Button pressed is: OK
Button pressed is: Cancel
廣告

© . All rights reserved.