PyQt 定义控件SwitchButton 指南

PyQt 定义控件SwitchButton 指南

SwitchButton 是一个自定义开关按钮控件,通常用于在用户界面中启用或禁用某些功能或选项。它是一种用户友好的控件,允许用户通过切换按钮的状态来控制应用程序的行为。

以下是 SwitchButton 的一些常见特征和用途:

  1. 切换功能SwitchButton 允许用户轻松地切换某种功能、选项或状态。它通常用于启用或禁用某些功能,例如启用/禁用声音、允许/禁止通知等。
  2. 图形外观SwitchButton 的外观通常是一个带有两种状态的图形按钮。通常,开启状态用一种颜色或图标表示,而关闭状态用另一种颜色或图标表示。开关按钮的外观可以根据应用程序的设计进行自定义。
  3. 交互性:用户可以单击 SwitchButton 来切换它的状态。这是一种直观的交互方式,用户可以快速了解功能是否启用。
  4. 逻辑绑定SwitchButton 可以与特定的逻辑或设置相关联。例如,当开关按钮处于开启状态时,相关功能将被启用,当它处于关闭状态时,相关功能将被禁用。
  5. 状态反馈:通常,SwitchButton 可以提供用户状态反馈,以指示当前状态是开启还是关闭。这可以是文本标签或颜色变化。

在许多图形用户界面工具包中,包括 PyQt5,开关按钮控件是一个常见的元素,用于增加用户界面的互动性和可配置性。您可以根据您的需求自定义开关按钮的外观和行为,并将其集成到您的应用程序中,以实现用户友好的功能切换。

实例程序

#!/usr/bin/env python


import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel,QWidget,QVBoxLayout
from PyQt5.QtCore import Qt,QRect
from PyQt5.QtGui import  QPainter,QFont,QBrush,QColor,QPen


class SwitchButton(QWidget):
    def __init__(self, parent=None):
        super(SwitchButton, self).__init__(parent)
        self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        #self.resize(70, 30)
        # SwitchButtonstate:True is ON,False is OFF
        self.state = False
        self.setFixedSize(80, 40)

    def mousePressEvent(self, event):
        '''
        set click event for state change
        '''
        super(SwitchButton, self).mousePressEvent(event)
        self.state = False if self.state else True
        self.update()

    def paintEvent(self, event):
        '''Set the button'''
        super(SwitchButton, self).paintEvent(event)

        # Create a renderer and set anti-aliasing and smooth transitions
        painter = QPainter(self)
        painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
        # Defining font styles
        font = QFont("Arial")
        font.setPixelSize(self.height()//3)
        painter.setFont(font)
        # SwitchButton state:ON
        if self.state:
            # Drawing background
            painter.setPen(Qt.NoPen)
            brush = QBrush(QColor('#bd93f9'))
            painter.setBrush(brush)
            # Top left corner of the rectangle coordinate
            rect_x = 0
            rect_y = 0
            rect_width = self.width()
            rect_height = self.height()
            rect_radius = self.height()//2
            painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)
            # Drawing slides circle
            painter.setPen(Qt.NoPen)
            brush.setColor(QColor('#ffffff'))
            painter.setBrush(brush)
            # Phase difference pixel point
            # Top left corner of the rectangle coordinate
            diff_pix = 3
            rect_x = self.width() - diff_pix - (self.height()-2*diff_pix)
            rect_y = diff_pix
            rect_width = (self.height()-2*diff_pix)
            rect_height = (self.height()-2*diff_pix)
            rect_radius = (self.height()-2*diff_pix)//2
            painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)

            # ON txt set
            painter.setPen(QPen(QColor('#ffffff')))
            painter.setBrush(Qt.NoBrush)
            painter.drawText(QRect(int(self.height()/3), int(self.height()/3.5), 50, 20), Qt.AlignLeft, 'ON')
        # SwitchButton state:OFF
        else:
            # Drawing background
            painter.setPen(Qt.NoPen)
            brush = QBrush(QColor('#525555'))
            painter.setBrush(brush)
            # Top left corner of the rectangle coordinate
            rect_x = 0
            rect_y = 0
            rect_width = self.width()
            rect_height = self.height()
            rect_radius = self.height()//2
            painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)

            # Drawing slides circle
            pen = QPen(QColor('#999999'))
            pen.setWidth(1)
            painter.setPen(pen)
            # Phase difference pixel point
            diff_pix = 3
            # Top left corner of the rectangle coordinate
            rect_x = diff_pix
            rect_y = diff_pix
            rect_width = (self.height()-2*diff_pix)
            rect_height = (self.height()-2*diff_pix)
            rect_radius = (self.height()-2*diff_pix)//2
            painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)

            # OFF txt set
            painter.setBrush(Qt.NoBrush)
            painter.drawText(QRect(int(self.width()*1/2), int(self.height()/3.5), 50, 20), Qt.AlignLeft, 'OFF')


def main():
    app = QApplication(sys.argv)
    window = QMainWindow()
    window.setGeometry(100, 100, 100, 290)
    window.setWindowTitle("Switch Button Example")
    switch1 = SwitchButton()
    switch2 = SwitchButton()
    layout = QVBoxLayout()
    layout.addWidget(switch1)
    layout.addWidget(switch2)
    window.setCentralWidget(QWidget())
    window.centralWidget().setLayout(layout)
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

效果如下所示:

在这里插入图片描述


End

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用 PyQt5 创建一个滑动开关控件的步骤和代码示例: 步骤1:安装 PyQt5 如果您尚未安装 PyQt5 库,可以使用以下命令来安装: ```shell pip install pyqt5 ``` 步骤2:创建 SwitchButton 类 ```python from PyQt5.QtCore import Qt, pyqtSignal from PyQt5.QtGui import QPainter, QColor, QBrush from PyQt5.QtWidgets import QWidget class SwitchButton(QWidget): # 定义信号 stateChanged = pyqtSignal(bool) def __init__(self, parent=None): super().__init__(parent) self.setFixedSize(50, 30) self.checked = False def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.NoPen) if self.checked: painter.setBrush(QColor(0, 255, 0)) else: painter.setBrush(QColor(255, 0, 0)) painter.drawRoundedRect(0, 0, self.width(), self.height(), self.height() // 2, self.height() // 2) def mousePressEvent(self, event): self.checked = not self.checked self.update() self.stateChanged.emit(self.checked) ``` 步骤3:使用 SwitchButton 控件 ```python import sys from PyQt5.QtWidgets import QApplication, QVBoxLayout, QLabel if __name__ == '__main__': app = QApplication(sys.argv) window = QWidget() layout = QVBoxLayout() label = QLabel('SwitchButton状态:') switch = SwitchButton() switch.stateChanged.connect(lambda state: label.setText('SwitchButton状态:{}'.format(state))) layout.addWidget(label) layout.addWidget(switch) window.setLayout(layout) window.show() sys.exit(app.exec_()) ``` 运行上述代码,即可看到一个滑动开关控件。当您单击该控件时,它将在“开”和“关”状态之间切换,并发出一个信号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Persus

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

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

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

打赏作者

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

抵扣说明:

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

余额充值