如何在 PyQt5 中改变 QMessageBox 的颜色 Python

How to change the color of QMessageBox in PyQt5 Python

我有下面的 QMessageBox 代码:

msgBox = QMessageBox()
reply = msgBox.question(self, 'Window Close', 'Are you sure you want to close the window?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

显示如下:

所以我添加了下面一行代码:

msgBox.setStyleSheet("QLabel{ color: white}")

但看起来它不起作用,因为它仍然是相同的黑色。如何将颜色更改为 while.

完整代码如下:

UI代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'app.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(790, 480)
        MainWindow.setStyleSheet("background-color: rgb(16, 16, 16);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 81, 31))
        self.pushButton.setStyleSheet("background-color: rgb(255, 255, 255);\n"
                                      "color: rgb(0, 0, 0);")
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 70, 601, 341))
        self.textEdit.setStyleSheet("background-color: rgb(255, 255, 255);\n"
                                    "color: rgb(0, 0, 0);")
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 790, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

app.py代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from src.app_ui import Ui_MainWindow


class HomeWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def closeEvent(self, event):
        msgBox = QMessageBox()
        msgBox.setStyleSheet("QLabel{ color: white}")
        reply = msgBox.question(self, 'Window Close', 'Are you sure you want to close the window?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


app = QApplication(sys.argv)
main_window = HomeWindow()
main_window.show()
sys.exit(app.exec_())

预期输出:

QMessageBox::question() 是一个静态方法,它创建一个 QMessageBox 对象,显示它和 returns 不同于您设置样式表的 msgBox 对象的选择。一种可能的解决方案是不使用 QMessageBox::question() 方法并使用 msgBox:

构建 window
def closeEvent(self, event):
    msgBox = QMessageBox(
        QMessageBox.Question,
        "window Close",
        "Are you sure you want to close the window?",
        buttons=QMessageBox.Yes | QMessageBox.No,
        parent=self,
    )
    msgBox.setDefaultButton(QMessageBox.No)
    msgBox.setStyleSheet("QLabel{ color: white}")
    msgBox.exec_()
    reply = msgBox.standardButton(msgBox.clickedButton())
    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()

Qt 样式表从父级到子级传播级联,这就是观察到这种样式的原因。如果不需要,那么一个可能的选择是不将其传递给父级:

reply = msgBox.question(
    None,
    "Window Close",
    "Are you sure you want to close the window?",
    QMessageBox.Yes | QMessageBox.No,
    QMessageBox.No,
)