您的位置:宽带测速网 > 编程知识 > PyQt5中怎么显示消息框

PyQt5中怎么显示消息框

2025-06-17 15:14来源:互联网 [ ]

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

import sysfrom PyQt5.QtWidgets import QApplication, QWidget, QMessageBoxclass 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等)来显示不同类型的消息框。