首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何动画图像/图标Widget

如何动画图像/图标Widget
EN

Stack Overflow用户
提问于 2019-03-20 10:44:11
回答 1查看 4.3K关注 0票数 3

我有一个自定义的‘加载’图像,我需要能够启动/停止一个旋转动画,而一些行动正在发生。我正在寻找代码,这些代码将向我展示如何做到这一点,但我在PyQt中找不到任何特定于旋转的东西。我已经有了以下代码,它使图像在单击按钮时旋转:

代码语言:javascript
复制
import os
import sys

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

class myApplication(QWidget):

def __init__(self, parent=None):
    super(myApplication, self).__init__(parent)

    self.imgPath = os.path.join('path/to/image','image.svg')
    pixmap = QPixmap(self.imgPath)
    diag = (pixmap.width()**2 + pixmap.height()**2)**0.5

    self.label = QLabel()
    self.label.setMinimumSize(diag, diag)
    self.label.setAlignment(Qt.AlignCenter)
    self.label.setPixmap(pixmap)

    #---- Prepare a Layout ----

    grid = QGridLayout()

    button = QPushButton('Rotate 15 degrees')
    button.clicked.connect(self.rotate_pixmap)

    grid.addWidget(self.label, 0, 0)
    grid.addWidget(button, 1, 0)

    self.setLayout(grid)

    self.rotation = 0

def rotate_pixmap(self):

    pixmap = QPixmap(self.imgPath)
    self.rotation += 15
    transform = QTransform().rotate(self.rotation)
    pixmap = pixmap.transformed(transform, Qt.SmoothTransformation)

    #---- update label ----

    self.label.setPixmap(pixmap)

if __name__ == '__main__':

app = QApplication(sys.argv)

instance = myApplication()  
instance.show()    

sys.exit(app.exec_())

我找到了this C++ code,它展示了如何使用QVariantAnimationQTransform创建旋转动画

代码语言:javascript
复制
class RotateMe : public QLabel {

    Q_OBJECT
public:
    explicit RotateMe(QWidget* parent = Q_NULLPTR) :
        QLabel(parent),
        pixmap(100, 100),
        animation(new QVariantAnimation )
    {
        resize(200, 200);
        pixmap.fill(Qt::red);

        animation->setDuration(10000);
        animation->setStartValue(0.0f);
        animation->setEndValue(90.0f);
        connect(animation, &QVariantAnimation::valueChanged, [=](const QVariant &value){
            qDebug()<<value;
            QTransform t;
            t.rotate(value.toReal());
            setPixmap(pixmap.transformed(t));
        });
        animation->start();
    }
private:
    QPixmap             pixmap;
    QVariantAnimation  *animation;
};

我怎么才能用python重写这个呢?我找不到任何例子说明QVariantAnimation是如何在PyQT中使用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-20 11:08:43

这一逻辑类似于我在下面展示的:

代码语言:javascript
复制
import os
from PyQt5 import QtCore, QtGui, QtWidgets

class RotateMe(QtWidgets.QLabel):
    def __init__(self, *args, **kwargs):
        super(RotateMe, self).__init__(*args, **kwargs)
        self._pixmap = QtGui.QPixmap()
        self._animation = QtCore.QVariantAnimation(
            self,
            startValue=0.0,
            endValue=360.0,
            duration=1000,
            valueChanged=self.on_valueChanged
        )

    def set_pixmap(self, pixmap):
        self._pixmap = pixmap
        self.setPixmap(self._pixmap)

    def start_animation(self):
        if self._animation.state() != QtCore.QAbstractAnimation.Running:
            self._animation.start()

    @QtCore.pyqtSlot(QtCore.QVariant)
    def on_valueChanged(self, value):
        t = QtGui.QTransform()
        t.rotate(value)
        self.setPixmap(self._pixmap.transformed(t))

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        label = RotateMe(alignment=QtCore.Qt.AlignCenter)
        img_path = os.path.join('path/to/image','image.svg')
        label.set_pixmap(QtGui.QPixmap(img_path))
        button = QtWidgets.QPushButton('Rotate')

        button.clicked.connect(label.start_animation)

        lay = QtWidgets.QVBoxLayout(self)
        lay.addWidget(label)
        lay.addWidget(button)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55258872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档