Python 在QMessageBox中设置QPushButton的背景
我有一个类,它在每次执行操作时都显示一个QMessageBox。我试图将QMessageBox中的按钮颜色设置为银色背景 此时按钮为蓝色,与QMessageBox的背景相同 我的问题是,如何使用这段代码:qtwidts.qApp.setStyleSheet(“QMessageBox QPushButton{background color:Silver;}”)将QMessageBox中的QPushButton颜色更改为银色 这是我的代码片段。我已经尝试将上述代码片段放入函数中,以便在单击按钮时,消息框中QPushButton的颜色将为银色。这有没有问题,因为它似乎没有做出任何改变。我应该把这个样式表功能放在代码的什么地方Python 在QMessageBox中设置QPushButton的背景,python,pyqt5,Python,Pyqt5,我有一个类,它在每次执行操作时都显示一个QMessageBox。我试图将QMessageBox中的按钮颜色设置为银色背景 此时按钮为蓝色,与QMessageBox的背景相同 我的问题是,如何使用这段代码:qtwidts.qApp.setStyleSheet(“QMessageBox QPushButton{background color:Silver;}”)将QMessageBox中的QPushButton颜色更改为银色 这是我的代码片段。我已经尝试将上述代码片段放入函数中,以便在单击按钮时,
self.canonicalAddressesButton.clicked.connect(self.canonical_data_parsed_notification)
def canonical_data_parsed_notification(self):
QtWidgets.QMessageBox.information(self.mainwindow, 'Notification', 'Canonical Address Data Has Been Parsed!', QtWidgets.QMessageBox.Ok)
QtWidgets.qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
在创建QMessageBox之前,应调用setStyleSheet()
方法。下面是一个简单的示例,说明了如何做到这一点:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, qApp, QMessageBox
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 300, 200)
button = QPushButton('Click me', self)
qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
button.clicked.connect(self.button_clicked)
def button_clicked(self):
QMessageBox.information(self, 'Notification', 'Text', QMessageBox.Ok)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = App()
widget.show()
sys.exit(app.exec_())
在创建QMessageBox之前,应调用setStyleSheet()
方法。下面是一个简单的示例,说明了如何做到这一点:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, qApp, QMessageBox
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 300, 200)
button = QPushButton('Click me', self)
qApp.setStyleSheet("QMessageBox QPushButton{background-color: Silver;}")
button.clicked.connect(self.button_clicked)
def button_clicked(self):
QMessageBox.information(self, 'Notification', 'Text', QMessageBox.Ok)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = App()
widget.show()
sys.exit(app.exec_())
在创建QMessageBox之前,您必须执行setStyleSheet命令!!!!!!我只是Python的初学者。我只是在学习,你必须在创建QMessageBox之前执行setStyleSheet命令!!!!!!我只是Python的初学者。我只是边走边学。我现在明白你的意思了。必须在def_init_(self)方法中设置它。是的,@eyllansc是正确的。这里是一个可以执行并看到结果的示例。我现在明白你的意思了。必须在def_init_(self)方法中设置它。是的,@eyllansc是正确的。这只是一个可以执行并看到结果的示例。