Skip to content

yjg30737/pyqt-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyqt-switch

PyQt Switch (O ) -> ( O)

You can choose the option to set the colorizing/moving animation.

Requirements

PyQt5 >= 5.8

Setup

python -m pip install pyqt-switch

Feature

  • Provide toggled(bool) signal
  • Set the animation with setAnimation(f: bool). Default is False.
  • Set the diameter of circle-shaped switch button with setCircleDiameter(diameter: int). Default is 20(px).
  • setChecked(bool) - Toggle the switch programmatically
  • isChecked() - Check switch is turned on

Example

Code Sample

from PyQt5.QtWidgets import QWidget, QFormLayout, QApplication, QLabel
from pyqt_switch import PyQtSwitch


class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.__initUi()

    def __initUi(self):
        self.__label = QLabel()
        self.__label.setText('No')

        switch = PyQtSwitch()
        switch.toggled.connect(self.__toggled)
        switch.setAnimation(True)
        # switch.setChecked(True)
        # switch.setCircleDiameter(40)

        # if switch.isChecked():
        #     print('Yes')
        # else:
        #     print('No')

        lay = QFormLayout()
        lay.addRow(self.__label, switch)
        self.setLayout(lay)

    def __toggled(self, f):
        if f:
            self.__label.setText('Yes')
        else:
            self.__label.setText('No')


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    example = Widget()
    example.show()
    app.exec_()

Result

switch_example.mp4

If you set the circle diameter to 40 with switch.setCircleDiameter(40)

image