PyQt4:使用QVariantAnimation在两个值之间设置动画

时间:2017-10-26 18:43:38

标签: python animation pyqt pyqt4

我有一个自定义进度条,我希望在它从一个值更改为另一个值时进行动画处理。当我得到一个值时,我重新绘制代表进度条的矩形 - 所以我认为这应该像动画值本身并重新绘制每个更改一样简单。我一直在查看Animation Framework documentation并且我非常确定将QVariantAnimation子类化为我所需要的 - 但是我找不到几乎没有Python示例,而且我有点丢失。

这是我到目前为止的地方(请原谅我这样的话):

import sys
from PyQt4 import QtGui, QtCore

class AnimateBetweenNums(QtCore.QVariantAnimation):
    def __init__(self):
        QtCore.QVariantAnimation.__init__(self)

    def updateCurrentValue(self, value):
        print value.toString()


class MyProgressbar(QtGui.QWidget):

    def __init__(self):
        super(MyProgressbar, self).__init__()
        self.initUI()

    def initUI(self):
        self.setMinimumSize(2, 2)
        self.value = 50

    def setValue(self, value):
        oldValue = 10
        newValue = 70

        anim = AnimateBetweenNums()
        anim.setStartValue(oldValue)
        anim.setEndValue(newValue)
        anim.setDuration(1000)
        anim.start()
        anim.valueChanged.connect(self.updateValue)

    def updateValue(self, value):
        self.value = value
        self.repaint()

    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawWidget(qp)
        qp.end()

    def drawWidget(self, qp):
        size = self.size()
        w = size.width()
        h = size.height()

        till = int(((w / 100.0) * self.value))

        #the bar
        qp.setPen(QtGui.QColor(255, 255, 255))
        qp.setBrush(QtGui.QColor(0, 228, 47))
        qp.drawRect(0, 0, till, h)

        #the box
        pen = QtGui.QPen(QtGui.QColor(75,80,100), 1, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setBrush(QtCore.Qt.NoBrush)
        qp.drawRect(0, 0, w - 1, h - 1)


class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        hbox = QtGui.QVBoxLayout()
        self.button10 = QtGui.QPushButton("10")
        hbox.addWidget(self.button10)
        self.button70 = QtGui.QPushButton("70")
        hbox.addWidget(self.button70)

        self.progress = MyProgressbar()
        hbox.addWidget(self.progress)

        self.setLayout(hbox)

        self.setGeometry(300, 300, 390, 210)
        self.show()

        self.button10.clicked.connect(self.changeValue10)
        self.button70.clicked.connect(self.changeValue70)

    def changeValue10(self, value):
        self.progress.setValue(10)
        self.progress.repaint()

    def changeValue70(self, value):
        self.progress.setValue(70)
        self.progress.repaint()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

以下是您的脚本的重写版本,希望能够实现您的目标。动画对象应该只创建一次,并且因为你使用Python 2和PyQt4,你需要确保正确转换任何QVariant值。我还更改了setValue()方法,以便从之前的值重新启动。

import sys
from PyQt4 import QtGui, QtCore

class AnimateBetweenNums(QtCore.QVariantAnimation):
    def __init__(self):
        QtCore.QVariantAnimation.__init__(self)

    def updateCurrentValue(self, value):
        print value.toString()

class MyProgressbar(QtGui.QWidget):
    def __init__(self):
        super(MyProgressbar, self).__init__()
        self.initUI()

    def initUI(self):
        self.setMinimumSize(2, 2)
        self.anim = AnimateBetweenNums()
        self.anim.setDuration(1000)
        self.anim.valueChanged.connect(self.updateValue)
        self.value = 50

    def setValue(self, value):
        self.anim.setStartValue(self.value)
        self.anim.setEndValue(value)
        self.anim.start()

    def updateValue(self, value):
        self.value = QtCore.QVariant(value).toInt()[0]
        self.repaint()

    def paintEvent(self, e):
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawWidget(qp)
        qp.end()

    def drawWidget(self, qp):
        size = self.size()
        w = size.width()
        h = size.height()
        till = int(((w / 100.0) * self.value))

        #the bar
        qp.setPen(QtGui.QColor(255, 255, 255))
        qp.setBrush(QtGui.QColor(0, 228, 47))
        qp.drawRect(0, 0, till, h)

        #the box
        pen = QtGui.QPen(QtGui.QColor(75,80,100), 1, QtCore.Qt.SolidLine)
        qp.setPen(pen)
        qp.setBrush(QtCore.Qt.NoBrush)
        qp.drawRect(0, 0, w - 1, h - 1)


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

    def initUI(self):
        hbox = QtGui.QVBoxLayout()
        self.button10 = QtGui.QPushButton("10")
        hbox.addWidget(self.button10)
        self.button70 = QtGui.QPushButton("70")
        hbox.addWidget(self.button70)
        self.progress = MyProgressbar()
        hbox.addWidget(self.progress)
        self.setLayout(hbox)
        self.setGeometry(300, 300, 390, 210)
        self.show()
        self.button10.clicked.connect(self.changeValue10)
        self.button70.clicked.connect(self.changeValue70)

    def changeValue10(self, value):
        self.progress.setValue(10)

    def changeValue70(self, value):
        self.progress.setValue(70)

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()