Skip to main content
 首页 » 编程设计

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

2024年10月01日8shanyou

我有一个自定义进度条,我想在它从一个值变为另一个值时设置动画。当我得到一个值时,我会重新绘制代表进度条的矩形 - 所以我认为这应该像为值本身设置动画并在每次更改时重新绘制一样简单。我一直在浏览 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() 

请您参考如下方法:

下面是您的脚本的重写版本,希望能达到您的预期。动画对象应该只创建一次,并且因为您使用的是 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()