PySide6控件:QFont设置、QColor调色板、QPixmap图像处理与QCursor光标自定义

QFont字体类

QFont 是一个用于表示 字体 的类。它可以设置字体的类型、大小、粗细、斜体、下划线等属性。from PySide6.QtGui import QFont。

复制代码
from PySide6.QtGui import QFont
from PySide6.QtWidgets import QApplication,QWidget,QLabel


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(300,300)
        self.label1 = QLabel('标签1',self)
        # font = QFont()
        font1 = QFont('宋体', 16)
        # 加粗
        font1.setBold(True)
        # 斜体
        font1.setItalic(True)
        # 下划线
        font1.setUnderline(True)
        self.label1.setFont(font1)

        self.label2 = QLabel('标签2',self)
        font2 = QFont()
        # 设置字体
        font2.setFamily('隶书')
        # 删除线
        font2.setStrikeOut(True)
        # 设置字体大小
        font2.setPointSize(18)
        self.label2.setFont(font2)

        self.label3 = QLabel('标签3',self)


        self.label1.setGeometry(0,0,100,20)
        self.label2.setGeometry(0,40,100,20)
        self.label3.setGeometry(0,80,100,40)



if __name__ == '__main__':
    app = QApplication([])

    afont = QFont('方正舒体',16)
    # 给应用程序设置默认字体
    app.setFont(afont)
    win = MyWindow()
    win.show()
    app.exec()

QColor颜色类

QColor 是 PySide6(以及 PyQt)中用于表示颜色的类。它可以用来表示 RGB、HSV、CMYK 等不同颜色模型中的颜色。from PySide6.QtGui import QColor,Qt

RGB(红 red,绿 gree,蓝 blue)值0-255

复制代码
from PySide6.QtGui import QColor,Qt

# 使用整数 RGB 值 (0-255)
color = QColor(255,0,0)
# 使用十六进制颜色代码
color = QColor('#FF0000')
# 字符串
color = QColor('red')
# 使用颜色的全局变量
color = QColor(Qt.GlobalColor.red)

from PySide6.QtGui import QColor,Qt

#红色
color = QColor(255,0,0)

red = color.red()
green = color.green()
blue = color.blue()
print('r',red,'g',green,'b',blue)
# 获取十六进制颜色代码
print(color.name())

# 设置为绿色
color.setRgb(0, 255, 0)
print('r',color.red(),'g',color.green(),'b',color.blue())

# 检查颜色是否有效。没有设置任何颜色值或非法颜色值返回False
print(f"Is color valid? {color.isValid()}")

QPalette调色板类

QPalette是用于管理 GUI 应用程序颜色主题的类。它定义了窗口部件(如按钮、标签、文本框等)的前景色、背景色和其他颜色属性。通过 QPalette,你可以自定义应用程序的外观和风格。

from PySide6.QtGui import QPalette

控件.palette() 获取调色板

控件.setPalette(QPalette)设置控件的调色板

两个重要的参数

ColorGroup 用于定义不同状态下控件的颜色:

激活状态(Active 获得焦点)

非激活状态(Inactive 失去焦点)

失效状态(Disabled 禁用)

Color Roles 定义了不同控件的颜色,例如:

背景色(QPalette.ColorRole.Window)

前景色(QPalette.ColorRole.WindowText)

按钮背景色(QPalette.ColorRole.Button)

按钮文本色(QPalette.ColorRole.ButtonText)

高亮色(QPalette.ColorRole.Highlight)

高亮文本色(QPalette.ColorRole.HighlightedText)

复制代码
from PySide6.QtGui import QPalette,QFont,QColor
from PySide6.QtWidgets import QApplication,QWidget,QLabel


if __name__ == '__main__':
    app = QApplication([])
    # 获取调色板
    palette = app.palette()
    # 设置窗口处于激活状态的背景颜色
    palette.setColor(palette.ColorGroup.Active,palette.ColorRole.Window,QColor('yellow'))
    # 设置窗口处于非激活状态的背景颜色
    palette.setColor(palette.ColorGroup.Inactive,palette.ColorRole.Window,QColor('white'))
    app.setPalette(palette)
    win = QWidget()
    win.resize(300,300)

    label = QLabel('Hello',win)
    font = QFont('宋体',20)
    label.setFont(font)
    label.setGeometry(10,10,100,20)

    lpalette = label.palette()
    # 激活状态 背景颜色变成粉色
    lpalette.setColor(lpalette.ColorGroup.Active,lpalette.ColorRole.Window,QColor('pink'))
    # 禁用状态背景颜色变成蓝色
    lpalette.setColor(lpalette.ColorGroup.Disabled,lpalette.ColorRole.Window,QColor('blue'))
    # 非激活状态字体变成红色
    lpalette.setColor(lpalette.ColorGroup.Inactive,lpalette.ColorRole.WindowText,QColor('red'))
    # 开启背景填充
    label.setAutoFillBackground(True)
    label.setPalette(lpalette)
    # label.setEnabled(True)

    win.show()
    app.exec()

QPixmap

QPixmap用于在屏幕上显示图像的类。常见用途:在 QLabel 或 QPushButton 中显示图像。功能具有图像缩放、旋转和裁剪。

可以读取和写入以下常见的图像格式,BMP、GIF、JPG/JPEG、PNG、PPM、XBM、XPM

复制代码
from PySide6.QtGui import QPixmap

qp1 = QPixmap()

# 长 100 宽 100
qp2 = QPixmap(100,100)
# 读入指定的图片
qp3 = QPixmap('imgs/a.png')

from PySide6.QtWidgets import QApplication,QWidget,QLabel
from PySide6.QtGui import QPixmap,Qt



if __name__ == "__main__":
    app = QApplication([])
    win = QWidget()
    win.resize(300,300)
    q = QPixmap()
    # 读入图像
    q.load('imgs\\a.png')
    # 保存图像
    q.save('out.png')
    # 转换成QImage图像
    qimage = q.toImage()
    print(type(qimage))
    # 获取宽度和高度
    print('宽',q.width(),'高',q.height())

    q1 = q.scaled(100,100,Qt.AspectRatioMode.KeepAspectRatio,
                  Qt.TransformationMode.SmoothTransformation)
    # Qt.TransformationMode
    # SmoothTransformation 平滑收缩算法,质量好,速度慢
    # FastTransformation 快速收缩算法,质量低,速度快

    # Qt.AspectRatioMode.
    # IgnoreAspectRatio 忽视宽、高
    # KeepAspectRatio 保持宽高比,可能会被比设置的尺寸小
    # KeepAspectRatioByExpanding 保持宽高比,超出区域会被填充

    label = QLabel('标签',win)
    label.setPixmap(q1)
    label.resize(100,100)
    
    win.show()
    app.exec()

from PySide6.QtWidgets import QApplication,QWidget,QLabel
from PySide6.QtGui import QPixmap,QColor


if __name__ == "__main__":
    app = QApplication([])
    win = QWidget()
    win.resize(300,300)
    q = QPixmap(100,100)
    q.fill(QColor('red'))

    label = QLabel('标签',win)
    label.setPixmap(q)
    label.resize(100,100)

    win.show()
    app.exec()

QIcon和QCursor

QIcon用于表示图标,可以支持多种格式的图标文件,如PNG、ICO、BMP等。

复制代码
from PySide6.QtGui import QIcon,QPixmap
from PySide6.QtWidgets import QApplication,QWidget,QPushButton

if __name__ == '__main__':
    app = QApplication([])
    win = QWidget()
    win.resize(300,300)
    icon = QIcon(QPixmap('imgs\\a.png'))
    # 添加图标
    # 按钮添加图标
    icon1 = QIcon()
    icon.addPixmap(QPixmap('imgs\\.png'))

    btn = QPushButton(win)
    btn.resize(100,60)

    btn.setIcon(icon)
    # 给窗口设置图标
    win.setWindowIcon(icon)
    win.show()
    app.exec()

QCursor设置光标的形状

|-------------------------|------------------------|----------------------|
| Qt.CursorShape. |||
| CrossCursor 十字光标 | ArrowCursor 默认光标 | WaitCursor 等待光标(沙漏) |
| PointingHandCursor 手型光标 | IBeamCursor 文本输入光标 I 形 | WhatsThisCursor 帮助光标 |

vb 复制代码
`from PySide6.QtGui import QCursor,Qt
from PySide6.QtWidgets import QApplication,QWidget,QPushButton

if __name__ == '__main__':
    app = QApplication([])

    win = QWidget()
    win.resize(300,300)

    # cursor = QCursor()
    # 设置光标
    # cursor.setShape(Qt.CursorShape.PointingHandCursor)
    win.setCursor(QCursor(Qt.CursorShape.WhatsThisCursor))
    win.show()
    app.exec()`
相关推荐
junyuz19 分钟前
Dify docker内网部署常见问题记录
python·docker
@HNUSTer26 分钟前
Python数据可视化科技图表绘制系列教程(一)
python·数据可视化·科技论文·专业制图·科研图表
reasonsummer1 小时前
【办公类-48-04】202506每月电子屏台账汇总成docx-5(问卷星下载5月范围内容,自动获取excel文件名,并转移处理)
python·excel
AmazingKO1 小时前
5分钟申请edu邮箱【方案本周有效】
python·chatgpt·ai编程·竹相左边·edu教育邮箱
幸存者1551 小时前
从零开始:亲手搭建你的第一个AI Agent(简单上手,先跑起来!)
python
点云SLAM1 小时前
Python中os模块详解
开发语言·前端·人工智能·python·计算机视觉
NON-JUDGMENTAL1 小时前
从零开始,搭建一个基于 Django 的 Web 项目
前端·python·django
PixelMind2 小时前
【LUT技术专题】图像自适应3DLUT代码讲解
人工智能·python·算法·lut
聚客AI2 小时前
AI大模型应用实战之GPU加速实战:手把手教你配置TensorFlow/PyTorch深度学习环境
人工智能·python·掘金·日新计划
疯狂的小强呀2 小时前
基于langchain的简单RAG的实现
python·langchain·rag检索增强