使用调色板需要先导入:from PySide6.QtGui import QPalette
调色板QPalette的源代码:
class QPalette(Shiboken.Object):class ColorGroup(enum.Enum):Active : QPalette.ColorGroup = ... # 0x0Normal : QPalette.ColorGroup = ... # 0x0Disabled : QPalette.ColorGroup = ... # 0x1Inactive : QPalette.ColorGroup = ... # 0x2NColorGroups : QPalette.ColorGroup = ... # 0x3Current : QPalette.ColorGroup = ... # 0x4All : QPalette.ColorGroup = ... # 0x5class ColorRole(enum.Enum):WindowText : QPalette.ColorRole = ... # 0x0Button : QPalette.ColorRole = ... # 0x1Light : QPalette.ColorRole = ... # 0x2Midlight : QPalette.ColorRole = ... # 0x3Dark : QPalette.ColorRole = ... # 0x4Mid : QPalette.ColorRole = ... # 0x5Text : QPalette.ColorRole = ... # 0x6BrightText : QPalette.ColorRole = ... # 0x7ButtonText : QPalette.ColorRole = ... # 0x8Base : QPalette.ColorRole = ... # 0x9Window : QPalette.ColorRole = ... # 0xaShadow : QPalette.ColorRole = ... # 0xbHighlight : QPalette.ColorRole = ... # 0xcHighlightedText : QPalette.ColorRole = ... # 0xdLink : QPalette.ColorRole = ... # 0xeLinkVisited : QPalette.ColorRole = ... # 0xfAlternateBase : QPalette.ColorRole = ... # 0x10NoRole : QPalette.ColorRole = ... # 0x11ToolTipBase : QPalette.ColorRole = ... # 0x12ToolTipText : QPalette.ColorRole = ... # 0x13PlaceholderText : QPalette.ColorRole = ... # 0x14Accent : QPalette.ColorRole = ... # 0x15NColorRoles : QPalette.ColorRole = ... # 0x16@overloaddef __init__(self) -> None: ...@overloaddef __init__(self, button: PySide6.QtCore.Qt.GlobalColor) -> None: ...@overloaddef __init__(self, button: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...@overloaddef __init__(self, button: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], window: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...@overloaddef __init__(self, palette: Union[PySide6.QtGui.QPalette, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor]) -> None: ...@overloaddef __init__(self, windowText: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], button: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], light: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], dark: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], mid: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], text: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], bright_text: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], base: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], window: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap]) -> None: ...@overloaddef __init__(self, windowText: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], window: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], light: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], dark: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], mid: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], text: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int], base: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...@staticmethoddef __copy__() -> None: ...def __lshift__(self, ds: PySide6.QtCore.QDataStream) -> PySide6.QtCore.QDataStream: ...def __rshift__(self, ds: PySide6.QtCore.QDataStream) -> PySide6.QtCore.QDataStream: ...def accent(self) -> PySide6.QtGui.QBrush: ...def alternateBase(self) -> PySide6.QtGui.QBrush: ...def base(self) -> PySide6.QtGui.QBrush: ...def brightText(self) -> PySide6.QtGui.QBrush: ...@overloaddef brush(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QBrush: ...@overloaddef brush(self, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QBrush: ...def button(self) -> PySide6.QtGui.QBrush: ...def buttonText(self) -> PySide6.QtGui.QBrush: ...def cacheKey(self) -> int: ...@overloaddef color(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QColor: ...@overloaddef color(self, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QColor: ...def currentColorGroup(self) -> PySide6.QtGui.QPalette.ColorGroup: ...def dark(self) -> PySide6.QtGui.QBrush: ...def highlight(self) -> PySide6.QtGui.QBrush: ...def highlightedText(self) -> PySide6.QtGui.QBrush: ...def isBrushSet(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole) -> bool: ...def isCopyOf(self, p: Union[PySide6.QtGui.QPalette, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor]) -> bool: ...def isEqual(self, cr1: PySide6.QtGui.QPalette.ColorGroup, cr2: PySide6.QtGui.QPalette.ColorGroup) -> bool: ...def light(self) -> PySide6.QtGui.QBrush: ...def link(self) -> PySide6.QtGui.QBrush: ...def linkVisited(self) -> PySide6.QtGui.QBrush: ...def mid(self) -> PySide6.QtGui.QBrush: ...def midlight(self) -> PySide6.QtGui.QBrush: ...def placeholderText(self) -> PySide6.QtGui.QBrush: ...def resolve(self, other: Union[PySide6.QtGui.QPalette, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor]) -> PySide6.QtGui.QPalette: ...def resolveMask(self) -> int: ...@overloaddef setBrush(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole, brush: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap]) -> None: ...@overloaddef setBrush(self, cr: PySide6.QtGui.QPalette.ColorRole, brush: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap]) -> None: ...@overloaddef setColor(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole, color: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...@overloaddef setColor(self, cr: PySide6.QtGui.QPalette.ColorRole, color: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...def setColorGroup(self, cr: PySide6.QtGui.QPalette.ColorGroup, windowText: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], button: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], light: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], dark: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], mid: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], text: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], bright_text: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], base: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap], window: Union[PySide6.QtGui.QBrush, PySide6.QtCore.Qt.BrushStyle, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor, PySide6.QtGui.QGradient, PySide6.QtGui.QImage, PySide6.QtGui.QPixmap]) -> None: ...def setCurrentColorGroup(self, cg: PySide6.QtGui.QPalette.ColorGroup) -> None: ...def setResolveMask(self, mask: int) -> None: ...def shadow(self) -> PySide6.QtGui.QBrush: ...def swap(self, other: Union[PySide6.QtGui.QPalette, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor]) -> None: ...def text(self) -> PySide6.QtGui.QBrush: ...def toolTipBase(self) -> PySide6.QtGui.QBrush: ...def toolTipText(self) -> PySide6.QtGui.QBrush: ...def window(self) -> PySide6.QtGui.QBrush: ...def windowText(self) -> PySide6.QtGui.QBrush: ...
QPalette 类有两个基本的概念,一个是颜色组ColorGroup,另一个是颜色角色ColorRole。
1、颜色角色ColorRole:
from PySide6.QtWidgets import QApplication, QLabel
from PySide6.QtGui import QColor, QPaletteapp = QApplication([])
palette = QPalette() # 创建调色板
color_red = QColor(255, 0, 0) # 定义红色
color_green = QColor(0, 255, 0) # 定义绿色
color_gray = QColor(130, 130, 130) # 定义灰色palette.setColor(QPalette.ColorRole.WindowText, color_red) # 将调色板的窗口文字设置为红色
palette.setColor(QPalette.ColorRole.Window, color_green) # 将调色板的窗口颜色设置为绿色label = QLabel("Hello World!") # 创建部件
label.setPalette(palette) # 将调色板应用到部件
label.show() # 显示部件app.exec()
运行结果:这里通过setColor的方法将调色板的窗口文字和背景颜色方便设置为了红色和绿色,并应用到部件。
还可使用 palette()的方法来获取一个既有的调色板,以及通过color()来获取调色板中的某一个具体颜色:
例如:
p = label.palette() # p是一个既有的部件label的调色板
color1 = p.color(p.ColorRole.WindowText) # 获取调色板中的窗口文字的颜色
demo:
import timefrom PySide6.QtWidgets import QApplication, QLabel
from PySide6.QtGui import QColor, QPaletteapp = QApplication([])
palette = QPalette() # 创建调色板
color_red = QColor(255, 0, 0) # 定义红色
color_green = QColor(0, 255, 0) # 定义绿色
color_gray = QColor(130, 130, 130) # 定义灰色palette.setColor(QPalette.ColorRole.WindowText, color_red) # 将调色板的窗口文字设置为红色
palette.setColor(QPalette.ColorRole.Window, color_green) # 将调色板的窗口颜色设置为绿色label = QLabel("Hello World!") # 创建部件
label.setPalette(palette) # 将调色板应用到部件
label.show() # 显示部件time.sleep(5) # 延时5秒p = label.palette() # 获取已有部件label的调色板
color1 = p.color(p.ColorRole.WindowText) # 获取窗口文字的颜色
color2 = p.color(p.ColorRole.Window) # 获取背景色palette.setColor(QPalette.ColorRole.WindowText, color2) # 重新设置调色板的窗口文字颜色
palette.setColor(QPalette.ColorRole.Window, color1) # 重新设置调色板的背景色
label.setPalette(palette) # 将调色板应用到部件app.exec()
运行之后可以看到,首先显示了一个绿底红字的部件。
5秒钟后,变成如下:
2、颜色组 ColorGroup:
ColorGroup 分为3 种情况
· 激活状态(Active,获得焦点)、
· 非激活状态(Inactive,失去焦点)
· 失效状态(Disabled,不可用),例如进行多窗口操作时.单击其中的一个窗口,可以在窗口中输人数据,则这个窗口是激活状态,其他窗口是非活跃状态。 将
ColorGroup 和ColorRole 结合起来,可以为控件不同部分不同状态设置不同的颜色。
import timefrom PySide6.QtWidgets import QApplication, QLabel
from PySide6.QtGui import QColor, QPaletteapp = QApplication([])
palette = QPalette() # 创建调色板
color_red = QColor(255, 30, 30) # 定义红色
color_green = QColor(30, 255, 30) # 定义绿色
color_gray = QColor(130, 130, 130) # 定义灰色
color_black = QColor(10, 10, 10) # 定义黑色palette.setColor(QPalette.ColorRole.WindowText, color_red) # 将调色板的窗口文字设置为红色
palette.setColor(QPalette.ColorRole.Window, color_green) # 将调色板的窗口颜色设置为绿色
disable_color_group = QPalette.ColorGroup(QPalette.ColorGroup.Disabled) # 获取Disabled的颜色组
palette.setColor(disable_color_group, QPalette.ColorRole.WindowText, color_black) # 将Disabled的颜色组的窗口文本设为灰色
palette.setColor(disable_color_group, QPalette.ColorRole.Window, color_gray) # 将Disabled的颜色组的窗口背景设为黑色label = QLabel("Hello World!") # 创建部件
label.setPalette(palette) # 将调色板应用到部件
label.show() # 显示部件time.sleep(3) # 延时3秒
label.setDisabled(True) # 禁用部件app.exec()
运行截图: