我正在使用以下托盘类为我的窗口获取深色主题。然而有趣的是,当我使用 QtextEdit
时,它按预期工作,背景变为黑色,字体变为白色。但是当涉及到 QLineEdit
时,背景为白色,文本也为白色。你能告诉我这段代码的问题在哪里吗?我不想为每个表单都添加内联样式,因为我使用了太多的 QlineEdits
。非常感谢。
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QFile
from PyQt5.QtGui import QPalette, QColor,QPainter
from PyQt5.QtCore import *
from PyQt5.QtCore import Qt
import Examiner
import MainLogin
#pallet
WHITE = QColor(255, 255, 255)
BLACK = QColor(0, 0, 0)
RED = QColor(255, 0, 0)
PRIMARY = QColor(53, 53, 53)
SECONDARY = QColor(35, 35, 35)
TERTIARY = QColor(42, 130, 218)
def css_rgb(color, a=False):
"""Get a CSS `rgb` or `rgba` string from a `QtGui.QColor`."""
return ("rgba({}, {}, {}, {})" if a else "rgb({}, {}, {})").format(*color.getRgb())
class QDarkPalette(QPalette):
"""Dark palette for a Qt application meant to be used with the Fusion theme."""
def __init__(self, *__args):
super().__init__(*__args)
# Set all the colors based on the constants in globals
self.setColor(QPalette.Window, QColor(53, 53, 53))
self.setColor(QPalette.WindowText, Qt.white)
self.setColor(QPalette.Base, QColor(35, 35, 35))
self.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
self.setColor(QPalette.ToolTipBase, QColor(25, 25, 25))
self.setColor(QPalette.ToolTipText, Qt.white)
self.setColor(QPalette.Text, Qt.white)
self.setColor(QPalette.Button, QColor(92, 184, 53))
self.setColor(QPalette.ButtonText, Qt.white)
self.setColor(QPalette.BrightText, Qt.red)
self.setColor(QPalette.Link, QColor(42, 130, 218))
self.setColor(QPalette.Highlight, QColor(42, 130, 218))
self.setColor(QPalette.HighlightedText, QColor(35, 35, 35))
self.setColor(QPalette.Active, QPalette.Button, QColor(92, 184, 53))
self.setColor(QPalette.Disabled, QPalette.ButtonText, Qt.darkGray)
self.setColor(QPalette.Disabled, QPalette.WindowText, Qt.darkGray)
self.setColor(QPalette.Disabled, QPalette.Text, Qt.darkGray)
self.setColor(QPalette.Disabled, QPalette.Light, QColor(53, 53, 53))
@staticmethod
def set_stylesheet(app):
"""Static method to set the tooltip stylesheet to a `QtWidgets.QApplication`."""
app.setStyleSheet("QToolTip {{"
"color: {white};"
"background-color: {tertiary};"
"border: 1px solid {white};"
"}}".format(white=css_rgb(WHITE), tertiary=css_rgb(TERTIARY)))
def set_app(self, app):
"""Set the Fusion theme and this palette to a `QtWidgets.QApplication`."""
app.setStyle("Fusion")
app.setPalette(self)
self.set_stylesheet(app)