使用调色板需要先导入:from PySide6.QtGui import QPalette
调色板QPalette的源代码:
class QPalette(Shiboken.Object):
class ColorGroup(enum.Enum):
Active : QPalette.ColorGroup = ... # 0x0
Normal : QPalette.ColorGroup = ... # 0x0
Disabled : QPalette.ColorGroup = ... # 0x1
Inactive : QPalette.ColorGroup = ... # 0x2
NColorGroups : QPalette.ColorGroup = ... # 0x3
Current : QPalette.ColorGroup = ... # 0x4
All : QPalette.ColorGroup = ... # 0x5
class ColorRole(enum.Enum):
WindowText : QPalette.ColorRole = ... # 0x0
Button : QPalette.ColorRole = ... # 0x1
Light : QPalette.ColorRole = ... # 0x2
Midlight : QPalette.ColorRole = ... # 0x3
Dark : QPalette.ColorRole = ... # 0x4
Mid : QPalette.ColorRole = ... # 0x5
Text : QPalette.ColorRole = ... # 0x6
BrightText : QPalette.ColorRole = ... # 0x7
ButtonText : QPalette.ColorRole = ... # 0x8
Base : QPalette.ColorRole = ... # 0x9
Window : QPalette.ColorRole = ... # 0xa
Shadow : QPalette.ColorRole = ... # 0xb
Highlight : QPalette.ColorRole = ... # 0xc
HighlightedText : QPalette.ColorRole = ... # 0xd
Link : QPalette.ColorRole = ... # 0xe
LinkVisited : QPalette.ColorRole = ... # 0xf
AlternateBase : QPalette.ColorRole = ... # 0x10
NoRole : QPalette.ColorRole = ... # 0x11
ToolTipBase : QPalette.ColorRole = ... # 0x12
ToolTipText : QPalette.ColorRole = ... # 0x13
PlaceholderText : QPalette.ColorRole = ... # 0x14
Accent : QPalette.ColorRole = ... # 0x15
NColorRoles : QPalette.ColorRole = ... # 0x16
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, button: PySide6.QtCore.Qt.GlobalColor) -> None: ...
@overload
def __init__(self, button: Union[PySide6.QtGui.QColor, PySide6.QtGui.QRgba64, Any, PySide6.QtCore.Qt.GlobalColor, str, int]) -> None: ...
@overload
def __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: ...
@overload
def __init__(self, palette: Union[PySide6.QtGui.QPalette, PySide6.QtCore.Qt.GlobalColor, PySide6.QtGui.QColor]) -> None: ...
@overload
def __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: ...
@overload
def __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: ...
@staticmethod
def __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: ...
@overload
def brush(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QBrush: ...
@overload
def 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: ...
@overload
def color(self, cg: PySide6.QtGui.QPalette.ColorGroup, cr: PySide6.QtGui.QPalette.ColorRole) -> PySide6.QtGui.QColor: ...
@overload
def 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: ...
@overload
def 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: ...
@overload
def 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: ...
@overload
def 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: ...
@overload
def 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, QPalette
app = 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 time
from PySide6.QtWidgets import QApplication, QLabel
from PySide6.QtGui import QColor, QPalette
app = 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 time
from PySide6.QtWidgets import QApplication, QLabel
from PySide6.QtGui import QColor, QPalette
app = 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()
运行截图: