我正在尝试将 GUI 调色板从深色更改为浅色。
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_())
这就是 PyQt5 所说的调色板可以设置为默认值(changeSkinLight 方法)的方式。
但是当我将功能分配给一个动作时。它只是运行 changeDarkSkin 而对 changeLightSkin 不做任何事情。如何像在 QtDesigner 中一样将调色板设置为默认值?