本篇 ShengYu 介紹 Python PyQt5 QMessageBox 用法與範例,在 Python GUI 程式設計中常常需要提出一個提示對話框告訴使用者一些訊息,例如:有錯誤發生或者離開程式前的確認對話框,所以這篇會大概地介紹常用的 QMessageBox 用法與範例。
以下的 Python PyQt5 QMessageBox 用法與範例將分為這幾部分,
- PyQt5 QMessageBox 提示訊息對話框
- PyQt5 QMessageBox 警告訊息對話框
- PyQt5 QMessageBox 錯誤訊息對話框
- PyQt5 QMessageBox 確定取消對話框
那就開始吧!
PyQt5 QMessageBox 提示訊息對話框
Python PyQt5 QMessageBox 提示訊息對話框基本用法如下,QMessageBox.information() 用來顯示一般資訊,如果第一個引數沒有父類別的 Widget 帶入可以使用 None,1
2
3
4
5
6
7
8#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QMessageBox)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    QMessageBox.information(None, 'my messagebox', 'hello world')
結果圖如下, 
  
接下來示範有父類別的 Widget 的寫法,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMessageBox)
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('Button example')
        self.setGeometry(50, 50, 200, 150)
        self.mybutton = QPushButton('Button', self)
        self.mybutton.move(60, 50)
        self.mybutton.clicked.connect(self.onButtonClick)
    def onButtonClick(self):
        reply = QMessageBox.information(self, 'my messagebox', 'hello world',
            QMessageBox.Ok | QMessageBox.Close, QMessageBox.Close)
        if reply == QMessageBox.Ok:
            print('Ok clicked.')
            self.mybutton.setText("Ok clicked.")
        else:
            print('Close clicked.')
            self.mybutton.setText("Close clicked.")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
結果圖如下, 
  
除了直接使用 QMessageBox.information() 將所有引數都帶入以外還有另一種寫法,就是先宣告一個 QMessageBox 變數,在一個一個地去使用對應的函式,最後再使用 exec(),如下所示,1
2
3
4
5
6
7
8msgbox = QMessageBox()
msgbox.setWindowTitle('my messagebox')
msgbox.setIcon(QMessageBox.Information)
msgbox.setText('hello world')
msgbox.setStandardButtons(QMessageBox.Ok | QMessageBox.Close)
msgbox.setDefaultButton(QMessageBox.Ok)
#msgbox.setDetailedText('detail ...')
reply = msgbox.exec()
PyQt5 QMessageBox 警告訊息對話框
PyQt5 要顯示警告訊息對話框的話用 QMessageBox.warning(),如果第一個引數沒有父類別的 Widget 帶入可以使用 None,用法如下,1
2
3
4
5
6
7
8#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QMessageBox)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    QMessageBox.warning(None, 'my messagebox', 'hello world')
結果圖如下, 
  
接下來示範有父類別的 Widget 的寫法,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMessageBox)
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('Button example')
        self.setGeometry(50, 50, 200, 150)
        self.mybutton = QPushButton('Button', self)
        self.mybutton.move(60, 50)
        self.mybutton.clicked.connect(self.onButtonClick)
    def onButtonClick(self):
        reply = QMessageBox.warning(self, 'my messagebox', 'hello world',
            QMessageBox.Ok | QMessageBox.Close, QMessageBox.Close)
        if reply == QMessageBox.Ok:
            print('Ok clicked.')
            self.mybutton.setText("Ok clicked.")
        else:
            print('Close clicked.')
            self.mybutton.setText("Close clicked.")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
結果圖如下, 
  
除了直接使用 QMessageBox.warning() 將所有引數都帶入以外還有另一種寫法,就是先宣告一個 QMessageBox 變數,在一個一個地去使用對應的函式,最後再使用 exec(),如下所示,1
2
3
4
5
6
7
8msgbox = QMessageBox(self)
msgbox.setWindowTitle('my messagebox')
msgbox.setIcon(QMessageBox.Warning)
msgbox.setText('hello world')
msgbox.setStandardButtons(QMessageBox.Ok | QMessageBox.Close)
msgbox.setDefaultButton(QMessageBox.Ok)
# msgbox.setDetailedText('detail ...')
reply = msgbox.exec()
PyQt5 QMessageBox 錯誤訊息對話框
PyQt5 要顯示錯誤訊息對話框的話用 messagebox.showerror(),如果第一個引數沒有父類別的 Widget 帶入可以使用 None,用法如下,1
2
3
4
5
6
7
8#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QMessageBox)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    QMessageBox.critical(None, 'my messagebox', 'hello world')
結果圖如下, 
  
接下來示範有父類別的 Widget 的寫法,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMessageBox)
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('Button example')
        self.setGeometry(50, 50, 200, 150)
        self.mybutton = QPushButton('Button', self)
        self.mybutton.move(60, 50)
        self.mybutton.clicked.connect(self.onButtonClick)
    def onButtonClick(self):
        reply = QMessageBox.critical(self, 'my messagebox', 'hello world',
            QMessageBox.Retry | QMessageBox.Abort | QMessageBox.Ignore, QMessageBox.Retry)
        if reply == QMessageBox.Retry:
            print('Retry clicked.')
            self.mybutton.setText("Retry clicked.")
        elif reply == QMessageBox.Abort:
            print('Abort clicked.')
            self.mybutton.setText("Abort clicked.")
        else:
            print('Ignore clicked.')
            self.mybutton.setText("Ignore clicked.")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
結果圖如下, 
  
除了直接使用 QMessageBox.critical() 將所有引數都帶入以外還有另一種寫法,就是先宣告一個 QMessageBox 變數,在一個一個地去使用對應的函式,最後再使用 exec(),如下所示,1
2
3
4
5
6
7
8msgbox = QMessageBox(self)
msgbox.setWindowTitle('my messagebox')
msgbox.setIcon(QMessageBox.Critical)
msgbox.setText('hello world')
msgbox.setStandardButtons(QMessageBox.Retry | QMessageBox.Abort | QMessageBox.Ignore)
msgbox.setDefaultButton(QMessageBox.Retry)
#msgbox.setDetailedText('detail ...')
reply = msgbox.exec()
PyQt5 QMessageBox 確定取消對話框
PyQt5 要顯示確定取消訊息對話框的話用 messagebox.askokcancel(),如果第一個引數沒有父類別的 Widget 帶入可以使用 None,用法如下,1
2
3
4
5
6
7
8#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QMessageBox)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    QMessageBox.question(None, 'my messagebox', 'hello world')
結果圖如下, 
  
接下來示範有父類別的 Widget 的寫法,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMessageBox)
class MyWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('Button example')
        self.setGeometry(50, 50, 200, 150)
        self.mybutton = QPushButton('Button', self)
        self.mybutton.move(60, 50)
        self.mybutton.clicked.connect(self.onButtonClick)
    def onButtonClick(self):
        reply = QMessageBox.question(self, 'my messagebox', 'hello world',
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            print('Yes clicked.')
            self.mybutton.setText("Yes clicked.")
        else:
            print('No clicked.')
            self.mybutton.setText("No clicked.")
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())
結果圖如下, 
  
除了直接使用 QMessageBox.question() 將所有引數都帶入以外還有另一種寫法,就是先宣告一個 QMessageBox 變數,在一個一個地去使用對應的函式,最後再使用 exec(),如下所示,1
2
3
4
5
6
7
8msgbox = QMessageBox(self)
msgbox.setWindowTitle('my messagebox')
msgbox.setIcon(QMessageBox.Question)
msgbox.setText('hello world')
msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgbox.setDefaultButton(QMessageBox.No)
# msgbox.setDetailedText('detail ...')
reply = msgbox.exec()
以上就是 Python PyQt5 QMessageBox 的介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!