kann mir jemand sagen, wieso die Printausgabe zwei mal was ausgibt.
Ich habe den Eindruck, dass die Animation doppelt gestartet wird. Genau so wie die Printausgabe
Code: Alles auswählen
class AnimButton(QPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.thread = WorkerThread(self)
        self.thread.start()
        self.thread.animation.connect(self.check_animation)
        color_button = self.palette().color(QPalette.Button).getRgb()
        color_highlight = self.palette().color(QPalette.Highlight).getRgb()
        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.animieren)
        self.color_flag = True
        self.color1 = QtGui.QColor(color_button[0], color_button[1], color_button[2])
        self.color2 = QtGui.QColor(color_highlight[0], color_highlight[1], color_highlight[2])
        self._animation = QtCore.QVariantAnimation(
            self,
            valueChanged=self._animate,
            startValue=0.00001,
            endValue=0.9999,
            duration=500
        )
    def check_animation(self):
        while self.animieren():
            self.timer.start()
        self.timer.stop()
        self.setStyleSheet('')
    def _animate(self, value):
        grad = "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 {color1}, stop:{value} {color2}, stop: 1.0 {color1});".format(
            color1=self.color1.name(), color2=self.color2.name(), value=value
        )
        self.setStyleSheet(grad)
    def animieren(self):
        if self.color_flag:
            self._animation.setDirection(QtCore.QAbstractAnimation.Forward)
            self._animation.start()
        else:
            self._animation.setDirection(QtCore.QAbstractAnimation.Backward)
            self._animation.start()
        self.color_flag = not self.color_flag
class WorkerThread(QThread):
    animation = pyqtSignal(bool)
    def __init__(self, parent=None):
        super(WorkerThread, self).__init__(parent)
        self._running = False
        self.animation.emit(False)
        self.animation_flag = False
        self.test = 0
    def run(self):
        self._running = True
        while self._running:
            self.doWork()
    def doWork(self):
        interval = 5000
        loop = QEventLoop()
        QTimer.singleShot(interval, loop.quit)
        loop.exec_()
        self.check_time()
        print(self.test)
        self.test += 1
    def check_time(self):
        zeit_start_animation = QTime.fromString("15:28", "hh:mm")
        zeit_stop_animation = zeit_start_animation.addSecs(30)
        now = QDateTime.currentDateTime().time()
        if now > zeit_start_animation and now < zeit_stop_animation:
            self.animation.emit(True)
