如何在pyqt5中获取切换值?

how to get toggle value in pyqt5?

我找到了以下教程 https://www.pythonguis.com/widgets/pyqt-toggle-widget/ 并且能够创建一个 Toggle 小部件,但我不知道如何检测值的变化。

能否请您告诉我如何检测以下代码中的切换值变化:

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_()

ToggleAnimatedToggle 是一个 QCheckBox with a custom painting, so if you want to detect when the state changes use the toggled 信号。

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)