how to get toggle value in pyqt5?

I found the following tutorial https://www.pythonguis.com/widgets/pyqt-toggle-widget/ and was able to create a Toggle widget, but I don’t know how to detect the value changes.

Can you please tell me how to detect toggle value change in below code:

import PyQt5
from PyQt5 import QtWidgets
from qtwidgets import Toggle, AnimatedToggle

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        toggle_1 = Toggle()
        toggle_2 = AnimatedToggle(
            checked_color="#FFB000",
            pulse_checked_color="#44FFB000"
        )

        container = QtWidgets.QWidget()
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(toggle_1)
        layout.addWidget(toggle_2)
        container.setLayout(layout)

        self.setCentralWidget(container)


app = QtWidgets.QApplication([])
w = Window()
w.show()
app.exec_()

Answer

The Toggle and AnimatedToggle is a QCheckBox with a custom painting, so if you want to detect when the state changes use the toggled signal.

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        toggle_1 = Toggle()
        toggle_2 = AnimatedToggle(
            checked_color="#FFB000", pulse_checked_color="#44FFB000"
        )

        toggle_1.toggled.connect(self.handle_toggled)
        toggle_2.toggled.connect(self.handle_toggled)

        container = QtWidgets.QWidget()
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(toggle_1)
        layout.addWidget(toggle_2)
        container.setLayout(layout)

        self.setCentralWidget(container)

    def handle_toggled(self, state):
        print(state)