Am attempting to change the GUI palette from dark to light.
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.gridLayout = QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
self.pushButton_2 = QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Dark"))
self.pushButton_2.setText(_translate("Form", "Light"))
self.pushButton.clicked.connect(self.changeSkinDark)
self.pushButton_2.clicked.connect(self.changeSkinLight)
def changeSkinDark(self):
darkpalette = QPalette()
darkpalette.setColor(QPalette.Window, QColor(41,44,51))
darkpalette.setColor(QPalette.WindowText, Qt.white)
darkpalette.setColor(QPalette.Base, QColor(15,15,15))
darkpalette.setColor(QPalette.AlternateBase, QColor(41,44,51))
darkpalette.setColor(QPalette.ToolTipBase, Qt.white)
darkpalette.setColor(QPalette.ToolTipText, Qt.white)
darkpalette.setColor(QPalette.Text, Qt.white)
darkpalette.setColor(QPalette.Button, QColor(41,44,51))
darkpalette.setColor(QPalette.ButtonText, Qt.white)
darkpalette.setColor(QPalette.BrightText, Qt.red)
darkpalette.setColor(QPalette.Highlight, QColor(100,100,225))
darkpalette.setColor(QPalette.HighlightedText, Qt.black)
app.setPalette(darkpalette)
def changeSkinLight(self):
palette = QGuiApplication.palette()
app.setPalette(palette)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setStyle('Fusion')
Form = QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
This is how PyQt5 says the palette can be set to default (changeSkinLight method).
But when I assign the functions to an action. It just runs the changeDarkSkin and does nothing on changeLightSkin. How can I set the palette to default like it was in QtDesigner?
You have to save the QPalette by default if you want to restore the application palette:
Note:
There seems to be a bug for certain versions of PyQt5 and python so a workaround is setting the palette to the window(thanks @S.Nick):