1

Есть такая анимация, подкиньте еще методов на примере этого кода.

import sys
from PyQt5.QtWidgets import QWidget, QApplication, QFrame, QPushButton, QGraphicsColorizeEffect
from PyQt5.QtCore import QPropertyAnimation
from PyQt5.QtGui import QColor


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.resize(500, 500)
        self.initUi()

    def initUi(self):
        self.color = QGraphicsColorizeEffect()
        self.color.setColor(QColor('black'))

        self.frame = QFrame(self)
        self.frame.setGeometry(100, 100, 100, 100)
        self.frame.setStyleSheet('background-color: black;')
        self.frame.setGraphicsEffect(self.color)

        self.button = QPushButton(self)
        self.button.setGeometry(250, 100, 100, 100)
        self.button.setText('Анимация')
        self.button.clicked.connect(self.Animation)

    def Animation(self):
        self.anim = QPropertyAnimation(self.color, b"color")
        self.anim.setDuration(3000)
        self.anim.setStartValue(QColor('black'))
        self.anim.setEndValue(QColor('red'))
        self.anim.start()


if __name__=="__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

1 ответ 1

1

Пример 1:

from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, 
        QLabel, QHBoxLayout, QSizePolicy)
from PyQt5.QtGui import QColor
from PyQt5.QtCore import QPropertyAnimation, pyqtProperty
import sys


class MyLabel(QLabel):
    def __init__(self, text):
        super().__init__(text)

    def _set_color(self, col):
        palette = self.palette()
        palette.setColor(self.foregroundRole(), col)
        self.setPalette(palette)

    color = pyqtProperty(QColor, fset=_set_color)


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):     
        hbox = QHBoxLayout(self)
        self.button = QPushButton("Start", self)
        self.button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        hbox.addWidget(self.button)
        hbox.addSpacing(40)

        self.label = MyLabel("Hello World")
        font = self.label.font()
        font.setPointSize(35)
        self.label.setFont(font)
        hbox.addWidget(self.label)

        self.anim = QPropertyAnimation(self.label, b"color")
        self.anim.setDuration(2500)
        self.anim.setLoopCount(2)
        self.anim.setStartValue(QColor(0, 0, 0))
        self.anim.setEndValue(QColor(255, 255, 255))
        self.button.clicked.connect(self.anim.start)


if __name__ == "__main__":
    app = QApplication([])
    ex = Example()
    ex.setGeometry(300, 300, 380, 250)
    ex.setWindowTitle('Color anim')
    ex.show()
    app.exec_()   

введите сюда описание изображения


Пример 2:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class BlinkButton(QPushButton):
    def __init__(self, *args, **kwargs):
        QPushButton.__init__(self, *args, **kwargs)
        self.default_color = self.getColor()

    def getColor(self):
        return self.palette().color(QPalette.Button)

    def setColor(self, value):
        if value == self.getColor():
            return
        palette = self.palette()
        palette.setColor(self.backgroundRole(), value)
        self.setAutoFillBackground(True)
        self.setPalette(palette)

    def reset_color(self):
        self.setColor(self.default_color)

    color = pyqtProperty(QColor, getColor, setColor)


class Widget(QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        layout = QVBoxLayout(self)
        self.button_stop = BlinkButton("Stop")
        layout.addWidget(self.button_stop)

        self.button_start = QPushButton("Start", self)
        layout.addWidget(self.button_start)

        self.animation = QPropertyAnimation(self.button_stop, b"color", self)
        self.animation.setDuration(1000)
        self.animation.setLoopCount(100)
        self.animation.setStartValue(self.button_stop.default_color)
        self.animation.setEndValue(self.button_stop.default_color)
        self.animation.setKeyValueAt(0.1, QColor(0,255,0))

        self.button_start.clicked.connect(self.animation.start)
        self.button_stop.clicked.connect(self.stop)

    def stop(self):
        self.animation.stop()
        self.button_stop.reset_color()

if __name__ == "__main__":
    app = QApplication([])
    w = Widget()
    w.resize(300,200)
    w.show()
    app.exec_()

введите сюда описание изображения

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.