This code is available:

from PyQt5.Qt import * class Example(QWidget): def __init__(self, parent=None): super(Example, self).__init__(parent) self.initUi() self.b.clicked.connect(self.chooseColor) def initUi(self): self.setStyleSheet('background : #000221;') self.mainLayout = QVBoxLayout() self.b = QPushButton('Color') self.b.setStyleSheet('color : #000000; background : rgb(255, 255, 255)') self.mainLayout.addWidget(self.b) self.setLayout(self.mainLayout) def chooseColor(self): dialog = QColorDialog(self) dialog.setStyleSheet('background : #7c7c7c;') if dialog.exec_(): red, green, blue, _ = dialog.currentColor().getRgb() self.b.setStyleSheet('background : {0}; font-size: 12pt; font-weight: 530; color: rgb({1}, {2}, {3});'.format(dialog.currentColor().name(), 255 - red, 255 - green, 255 - blue)) if __name__ == '__main__': app = QApplication([]) c = Example() c.show() app.exec() 

However, when the style changes, the Ok and Cancel buttons remain with the color of the parent widget. How to fix it?

  • @Twiss Thank you. - my diamonds dancing

1 answer 1

remove self from QColorDialog(self) i.e. change to QColorDialog()

 from PyQt5.Qt import * class Example(QWidget): def __init__(self, parent=None): super(Example, self).__init__(parent) self.initUi() self.b.clicked.connect(self.chooseColor) def initUi(self): self.setStyleSheet('background : #000221;') self.mainLayout = QVBoxLayout() self.b = QPushButton('Color') self.b.setStyleSheet('color : #000000; background : rgb(255, 255, 255)') self.mainLayout.addWidget(self.b) self.setLayout(self.mainLayout) def chooseColor(self): dialog = QColorDialog() dialog.setStyleSheet('background : #7c7c7c;') if dialog.exec_(): red, green, blue, _ = dialog.currentColor().getRgb() self.b.setStyleSheet('background : {0}; font-size: 12pt; font-weight: 530; color: rgb({1}, {2}, {3});'.format(dialog.currentColor().name(), 255 - red, 255 - green, 255 - blue)) if __name__ == '__main__': app = QApplication([]) c = Example() c.show() app.exec()