pyside6的调色板QPalette的简单应用

使用调色板需要先导入: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()

 运行截图:

 另:实践中发现,如果部件的样式表中有自定义的颜色,那么就不能在调色板中改变相对应的颜色。比如在样式表中仅定义了背景色,那么调色板中改变背景色就不会起作用,但是在调色板中改变文字颜色是可以的。就是说,样式表的执行顺序比调色板要晚,即使在调色板中设定了颜色,也会在执行样式表的时候又改回去了。

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PySide6中的调色板(Palette)用于定义窗口和窗口中控件的颜色。调色板类是QPalette,可以通过设置不同的颜色组(Color Group)和颜色角色(Color Role)来定义调色板。 下面是一个简单的例子: ```python from PySide6.QtGui import QColor, QPalette from PySide6.QtWidgets import QApplication, QLabel, QWidget app = QApplication([]) widget = QWidget() # 创建调色板对象 palette = QPalette() # 设置窗口背景颜色 palette.setColor(QPalette.Window, QColor(53, 53, 53)) widget.setPalette(palette) # 创建标签并设置文本和前景色 label = QLabel("Hello, World!") palette.setColor(QPalette.WindowText, QColor(255, 255, 255)) label.setPalette(palette) widget.show() app.exec() ``` 在上面的例子中,我们创建了一个调色板对象,并设置了窗口背景颜色和标签的前景色。 调色板中的颜色组包括窗口(Window)、按钮(Button)、文本(Text)等,颜色角色包括前景色(WindowText)、背景色(Window)、文本颜色(ButtonText)等。 除了使用默认的颜色组和颜色角色外,我们还可以使用自定义的颜色组和颜色角色。例如,我们可以通过以下方式创建一个自定义的颜色组: ```python # 创建自定义颜色组 customColorGroup = QPalette.ColorGroup.Normal palette.setColor(customColorGroup, QPalette.ColorRole.Foreground, QColor(255, 0, 0)) ``` 在这个例子中,我们创建了一个自定义颜色组,并设置了其前景色为红色。 更多关于PySide6调色板的使用方法可以参考官方文档:https://doc.qt.io/qtforpython/PySide6/QtGui/QPalette.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

深蓝海拓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值