温馨提示×

PyQt5中怎么显示消息框

小亿
85
2024-03-12 17:41:41
栏目: 编程语言

在PyQt5中显示消息框可以使用QMessageBox类。以下是一个简单的例子:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox

class Example(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Message box example')

        self.showMessageBox()

    def showMessageBox(self):
        QMessageBox.information(self, 'Information', 'This is an information message box')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

在这个例子中,我们创建了一个简单的窗口,并在窗口中显示了一个信息消息框。您可以通过调用QMessageBox的静态方法(如information、question、warning等)来显示不同类型的消息框。

0