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

我有一个QMessageBox的以下代码:

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

如下所示:

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

所以我在代码行下面添加了

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);")
        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);\n"
                                    "color: rgb(0,0);")
        self.textEdit.setobjectname("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(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,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_())

预期输出:

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

iCMS 回答:如何在PyQt5 Python中更改QMessageBox的颜色

QMessageBox::question()是一个静态方法,它创建一个QMessageBox对象,将其显示并返回与设置样式表的msgBox对象不同的选择。一种可能的解决方案是不使用QMessageBox :: question()方法,而使用msgBox构建窗口:

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",QMessageBox.Yes | QMessageBox.No,QMessageBox.No,)
本文链接:https://www.f2er.com/1588534.html

大家都在问