QMessageBox是一种通用的弹出式对话框,用于显示提示消息,允许用户点击不同的消息框按钮做出不同的判断。Pyside6提供了QMessageBox的操作函数,关于QMessageBox的资料可以参考下面的文档

https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QMessageBox.html#qmessagebox

QMessageBox使用

5种基本消息

Pyside6的QMessageBox提供了5种用户常用的消息,分别是about(关于)、critical(严重错误)、information(消息)、question(问答)、warning(警告)。

def about_msg(self):
    QMessageBox.about(self, "关于消息框", "关于消息")

def critical_msg(self):
    ret = QMessageBox.critical(self, "严重错误消息框", "这是一个严重错误",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
    if ret == QMessageBox.StandardButton.Ok:
        print("选择了OK")
    else:
        print("选择了NO")

def information_msg(self):
    ret = QMessageBox.information(self, "消息框", "这是一个消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
    if ret == QMessageBox.StandardButton.Ok:
        print("选择了OK")
    else:
        print("选择了NO")

def question_msg(self):
    ret = QMessageBox.question(self, "问答框", "这是一个问答消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
    if ret == QMessageBox.StandardButton.Ok:
        print("选择了OK")
    else:
        print("选择了NO")

def warning_msg(self):
    ret = QMessageBox.warning(self, "警告框", "这是一个警告消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
    if ret == QMessageBox.StandardButton.Ok:
        print("选择了OK")
    else:
        print("选择了NO")

Pyside6 QMessageBox-LMLPHP

自定义消息框

除了上面5种基本消息框之外,我们还可以自己定义消息框

msgBox = QMessageBox()
msgBox.setText("文本内容已经改变.") # 设置消息框文本
msgBox.setInformativeText("是否需要保存?") # 设置消息框内容
msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) # 设置消息框标准按钮
msgBox.setDefaultButton(QMessageBox.Save) # 设置消息框默认按钮
ret = msgBox.exec() # 等待消息框退出
if ret == QMessageBox.Save:
    # Save was clicked
    print("选择了保存")
elif ret == QMessageBox.Discard:
    # Don't Save was clicked
    print("选择了放弃")
elif ret == QMessageBox.Cancel:
    # Cancel was clicked
    print("选择了取消")
else:
    # should never be reached
    print("没有选择")

Pyside6 QMessageBox-LMLPHP

QMessageBox标准按钮

QMessageBox定义了标准按钮,每个按钮都有一个特定的值,当用户设置了标准按钮之后,就可以通过消息框的返回值判断用户选择了哪个按钮,做出不同的响应。

ButtonMask               : QMessageBox.StandardButton = ... # -0x301
NoButton                 : QMessageBox.StandardButton = ... # 0x0
Default                  : QMessageBox.StandardButton = ... # 0x100
Escape                   : QMessageBox.StandardButton = ... # 0x200
FlagMask                 : QMessageBox.StandardButton = ... # 0x300
FirstButton              : QMessageBox.StandardButton = ... # 0x400
Ok                       : QMessageBox.StandardButton = ... # 0x400
Save                     : QMessageBox.StandardButton = ... # 0x800
SaveAll                  : QMessageBox.StandardButton = ... # 0x1000
Open                     : QMessageBox.StandardButton = ... # 0x2000
Yes                      : QMessageBox.StandardButton = ... # 0x4000
YesAll                   : QMessageBox.StandardButton = ... # 0x8000
YesToAll                 : QMessageBox.StandardButton = ... # 0x8000
No                       : QMessageBox.StandardButton = ... # 0x10000
NoAll                    : QMessageBox.StandardButton = ... # 0x20000
NoToAll                  : QMessageBox.StandardButton = ... # 0x20000
Abort                    : QMessageBox.StandardButton = ... # 0x40000
Retry                    : QMessageBox.StandardButton = ... # 0x80000
Ignore                   : QMessageBox.StandardButton = ... # 0x100000
Close                    : QMessageBox.StandardButton = ... # 0x200000
Cancel                   : QMessageBox.StandardButton = ... # 0x400000
Discard                  : QMessageBox.StandardButton = ... # 0x800000
Help                     : QMessageBox.StandardButton = ... # 0x1000000
Apply                    : QMessageBox.StandardButton = ... # 0x2000000
Reset                    : QMessageBox.StandardButton = ... # 0x4000000
LastButton               : QMessageBox.StandardButton = ... # 0x8000000
RestoreDefaults          : QMessageBox.StandardButton = ... # 0x8000000

程序

界面程序

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>247</width>
    <height>252</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QPushButton" name="about_msg_btn">
        <property name="text">
         <string>关于消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QPushButton" name="critical_msg_btn">
        <property name="text">
         <string>严重错误消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_3">
      <item>
       <widget class="QPushButton" name="information_msg_btn">
        <property name="text">
         <string>消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_4">
      <item>
       <widget class="QPushButton" name="question_msg_btn">
        <property name="text">
         <string>问答消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_5">
      <item>
       <widget class="QPushButton" name="warning_msg_btn">
        <property name="text">
         <string>警告消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout_6">
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>自定义消息框</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>247</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

主程序

# Import Qt libraries
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
# Import UI developed in Qt Creator
from messagebox_ui import Ui_MainWindow  # 导入界面
# Import PseudoSensor
# Import system tools and datetime
import sys
import statistics
import time
from datetime import datetime

# Create and start the Qt application
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        
        # 设置界面为用户设计的界面
        self.ui = Ui_MainWindow() 
        self.ui.setupUi(self) 

        self.ui.about_msg_btn.clicked.connect(self.about_msg)
        self.ui.critical_msg_btn.clicked.connect(self.critical_msg)
        self.ui.information_msg_btn.clicked.connect(self.information_msg)
        self.ui.question_msg_btn.clicked.connect(self.question_msg)
        self.ui.warning_msg_btn.clicked.connect(self.warning_msg)
        self.ui.pushButton.clicked.connect(self.custom_msg)


    def custom_msg(self):
        msgBox = QMessageBox()
        msgBox.setText("文本内容已经改变.") # 设置消息框文本
        msgBox.setInformativeText("是否需要保存?") # 设置消息框内容
        msgBox.setStandardButtons(QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel) # 设置消息框标准按钮
        msgBox.setDefaultButton(QMessageBox.Save) # 设置消息框默认按钮
        ret = msgBox.exec() # 等待消息框退出
        if ret == QMessageBox.Save:
            # Save was clicked
            print("选择了保存")
        elif ret == QMessageBox.Discard:
            # Don't Save was clicked
            print("选择了放弃")
        elif ret == QMessageBox.Cancel:
            # Cancel was clicked
            print("选择了取消")
        else:
            # should never be reached
            print("没有选择")


    def about_msg(self):
        QMessageBox.about(self, "关于消息框", "关于消息")

    def critical_msg(self):
        ret = QMessageBox.critical(self, "严重错误消息框", "这是一个严重错误",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
        if ret == QMessageBox.StandardButton.Ok:
            print("选择了OK")
        else:
            print("选择了NO")

    def information_msg(self):
        ret = QMessageBox.information(self, "消息框", "这是一个消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
        if ret == QMessageBox.StandardButton.Ok:
            print("选择了OK")
        else:
            print("选择了NO")

    def question_msg(self):
        ret = QMessageBox.question(self, "问答框", "这是一个问答消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
        if ret == QMessageBox.StandardButton.Ok:
            print("选择了OK")
        else:
            print("选择了NO")

    def warning_msg(self):
        ret = QMessageBox.warning(self, "警告框", "这是一个警告消息",QMessageBox.StandardButton.Ok,QMessageBox.StandardButton.No)
        if ret == QMessageBox.StandardButton.Ok:
            print("选择了OK")
        else:
            print("选择了NO")

    def closeAndExit(self):
        sys.exit()

if __name__ == "__main__":
    app = QApplication(sys.argv) # 初始化QApplication

    # 初始化界面并显示界面
    window = MainWindow() 
    window.show() 
    window.setFixedSize(window.width(), window.height())
    sys.exit(app.exec())
10-20 16:44