PyQt5 – 从未选中的复选框为已按下的指示器设置背景图片

yizhihongxing

要实现从未选中的复选框为已按下的指示器设置背景图片,需要使用PyQt5中的QSS(Qt Style Sheet)来设置样式。以下是详细的使用攻略:

  1. 导入必要的依赖项
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QCheckBox, QApplication
  1. 创建自定义复选框类
class CustomCheckBox(QCheckBox):
    def __init__(self):
        super().__init__()

        # 设置复选框的默认样式
        self.setStyleSheet("QCheckBox::indicator {width: 20px; height: 20px;}");
  1. 重写paintEvent方法
def paintEvent(self, event):
    # 绘制复选框指示器的背景
    if self.isChecked():
        bg_image = QPixmap('checked.png')
    else:
        bg_image = QPixmap('unchecked.png')

    painter = QPainter(self)
    painter.drawPixmap(0, 0, bg_image.width(), bg_image.height(), bg_image)

    # 使用默认的绘制方法
    super().paintEvent(event)
  1. 设置样式表
app = QApplication(sys.argv)
checkbox = CustomCheckBox()
checkbox.setStyleSheet(
    "QCheckBox::indicator:checked {background-color: transparent;}"
    "QCheckBox::indicator:unchecked {background-color: transparent;}"
)

在这里,我们使用了两个样式表来覆盖复选框指示器的背景为透明,否则会和背景图片冲突。

接下来是两个示例说明:

示例1:用PyQt5设置具有背景图片的复选框

app = QApplication(sys.argv)

checkbox = CustomCheckBox()
checkbox.setGeometry(50, 50, 100, 100)
checkbox.show()

app.setStyleSheet(
    "QCheckBox::indicator:checked {background-color: transparent;}"
    "QCheckBox::indicator:unchecked {background-color: transparent;}"
)

sys.exit(app.exec_())

示例2:用PyQt5为复选框应用动画

app = QApplication(sys.argv)

checkbox = CustomCheckBox()
checkbox.setGeometry(50, 50, 100, 100)
checkbox.show()

app.setStyleSheet(
    "QCheckBox::indicator:checked {background-color: transparent;}"
    "QCheckBox::indicator:unchecked {background-color: transparent;}"
    "QCheckBox::indicator:checked {"
    "   border: 0px solid #2f9e99;"
    "   border-radius: 10px;"
    "   background-color: #2f9e99;"
    "   width: 20px;"
    "   height: 20px;"
    "}"
    "QCheckBox::indicator:checked:pressed {"
    "   background-color: #19ad9d;"
    "   border: 1px solid #19ad9d;"
    "}"
    "QCheckBox::indicator:hover {"
    "   border: 2px solid #19ad9d;"
    "}"
)

animation1 = QPropertyAnimation(checkbox, b"geometry")
animation1.setDuration(500)
animation1.setStartValue(QRect(50, 50, 100, 100))
animation1.setEndValue(QRect(150, 50, 100, 100))
animation1.setEasingCurve(QEasingCurve.OutQuad)

animation2 = QPropertyAnimation(checkbox, b"geometry")
animation2.setDuration(500)
animation2.setStartValue(QRect(150, 50, 100, 100))
animation2.setEndValue(QRect(50, 50, 100, 100))
animation2.setEasingCurve(QEasingCurve.InQuad)

animation_group = QSequentialAnimationGroup()
animation_group.addAnimation(animation1)
animation_group.addAnimation(animation2)
animation_group.setLoopCount(-1)
animation_group.start()

sys.exit(app.exec_())

此示例使用了QPropertyAnimation和QSequentialAnimationGroup来实现动画效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5 – 从未选中的复选框为已按下的指示器设置背景图片 - Python技术站

(0)
上一篇 2023年5月11日
下一篇 2023年5月11日

相关文章

  • PyQt5 主要类

    下面是关于Python的PyQt5主要类的完整使用攻略。 PyQt5 主要类概述 PyQt5是一个基于Python的GUI编程工具,其主要类包括: QApplication:应用程序 Qwidget:窗口或控件 QPushButton:按钮 QMenu:弹出菜单 QAction:在菜单、工具栏或自定义快捷键中可用的抽象用户操作 QLabel:文本或图像标签 …

    python 2023年5月12日
    00
  • PyQt5 QSpinBox – 获取线程

    PyQt5是Python中很流行的GUI工具包,其中PyQt5 QSpinBox是一种可以让用户通过鼠标或键盘来选择整数的控件。在实际应用开发中,我们可能需要在一个线程中获取QSpinBox的值。下面就详细介绍在PyQt5中,如何获取QSpinBox的值并在线程中使用它。 1. PyQt5 QSpinBox 在介绍如何在线程中获取QSpinBox的值之前,我…

    python 2023年5月12日
    00
  • PyQt5 QDateTimeEdit – 设置包覆属性

    首先我们来了解一下 PyQt5 中的 QDateTimeEdit 控件。 QDateTimeEdit 是一个用于编辑时间和日期的控件,用户可以使用它手动输入,也可以通过弹出的日历控制选择日期和时间。同时,QDateTimeEdit 还支持格式化日期和时间的显示。 接下来我们来看一下如何设置 QDateTimeEdit 的属性值。 设置 QDateTimeEd…

    python 2023年5月12日
    00
  • PyQt5 QDateEdit – 递增日期

    Python中的PyQt5库提供了QDateEdit控件来使用户能够编辑日期。本文将讲解如何使用PyQt5的QDateEdit控件来递增日期,包括两个示例说明。 安装PyQt5 在开始使用PyQt5控件之前,必须先安装PyQt5。可以通过pip来完成安装,具体步骤如下: pip install PyQt5 QDateEdit控件 QDateEdit控件是Py…

    python 2023年5月12日
    00
  • PyQt5标签 – 检查标签是否为窗口类型

    下面我来详细讲解Python的PyQt5标签 – 检查标签是否为窗口类型的完整使用攻略。 简介 PyQt5标签是一种用于创建GUI界面的Python库,它使用了Qt图形用户界面框架,可以创建跨平台的应用程序,包括Windows、Linux和MacOS等。在PyQt5中,标签是一种常用的界面控件,用于显示文本或图像等信息。在本文中,我们主要介绍如何检查标签是否…

    python 2023年5月11日
    00
  • PyQt5的QSpinBox – 显示在上层

    让我来详细讲解Python的“PyQt5的QSpinBox-显示在上层”的完整使用攻略。 1. 安装PyQt5 首先,你需要安装PyQt5库。你可以使用pip命令来安装PyQt5: pip install PyQt5 2. 创建QSpinBox控件并在上层显示 QSpinBox是PyQt5中的一个控件,可以用于数字输入。在创建QSpinBox控件时,我们需要…

    python 2023年5月12日
    00
  • PyQt5 QCalendarWidget 设置描述属性

    PyQt5中的QCalendarWidget是一个用于显示和选择日期的控件。除了基本的日期选择功能外,QCalendarWidget还具有设置描述属性的功能,可以帮助用户更好地理解显示的日期。 以下是使用QCalendarWidget设置描述属性的完整攻略: 1. 设置描述属性 使用setDescription(date, text)方法可以为指定日期设置描…

    python 2023年5月12日
    00
  • PyQt5 QCheckBox小工具

    下面是关于Python PyQt5 QCheckBox小工具的完整使用攻略。 概述 QCheckBox是PyQt5中的一个小工具,是一个复选框,可用于表示On或Off状态。用户可以通过选中或取消选中复选框来改变复选框的状态。 使用方法 1. 导入PyQt5库和其他必要库 使用PyQt5 QCheckBox小工具,首先需要导入PyQt5库。如果使用PyChar…

    python 2023年5月13日
    00
合作推广
合作推广
分享本页
返回顶部