QMessageBox
是Qt中用于显示消息框的类,可以用于显示各种类型的消息、提示、警告和错误信息。以下是几个常见的QMessageBox
的用法示例:
-
显示消息框:
from PyQt5.QtWidgets import QApplication, QMessageBox # 创建应用程序 app = QApplication([]) # 显示消息框 QMessageBox.information(None, "Information", "This is an information message") # 运行应用程序 app.exec_()
-
显示警告框:
# 显示警告框 QMessageBox.warning(None, "Warning", "This is a warning message")
-
显示错误框:
# 显示错误框 QMessageBox.critical(None, "Error", "This is an error message")
-
显示询问框:
# 显示询问框 reply = QMessageBox.question(None, "Question", "Do you want to continue?", QMessageBox.Yes | QMessageBox.No) if reply == QMessageBox.Yes: print("Yes button clicked") else: print("No button clicked")
-
自定义按钮:
# 自定义按钮 custom_button = QMessageBox.question(None, "Custom Buttons", "Choose an option:", QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel) if custom_button == QMessageBox.Yes: print("Yes button clicked") elif custom_button == QMessageBox.No: print("No button clicked") else: print("Cancel button clicked")
这些示例展示了QMessageBox
的基本用法,包括显示不同类型的消息框、自定义按钮以及根据用户的选择执行相应的操作。QMessageBox
是Qt中用于显示消息的常用类,可以方便地在应用程序中添加消息提示功能。