python - QPropertyAnimation 不适用于子部件

标签 python qt animation pyqt

下面的代码没有按预期为按钮设置动画。但如果按钮是独立的并且在它是子部件时停止工作,它会起作用。我在这里做错了什么?

我正在 Ubuntu 上尝试这个。

class TestWindow(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.button = QtGui.QPushButton("Ok")
        self.button.setParent(self)
        self.button.setGeometry(QtCore.QRect(0,0,50,50))
        self.button.clicked.connect(self.anim)

    def anim(self):

        animation = QtCore.QPropertyAnimation(self.button, "geometry")
        animation.setDuration(10000)
        animation.setStartValue(QtCore.QRect(0,0,0,0))
        animation.setEndValue(QtCore.QRect(0,0,200,200))
        animation.start()

if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)

        r = TestWindow()
        r.show()

        sys.exit(app.exec_())

最佳答案

我刚刚在带有 PySide 的 Ubuntu 10.04 上试过它。尝试保留对动画对象的引用,它解决了这里的问题:

def anim(self):

    animation = QtCore.QPropertyAnimation(self.button, "geometry")
    animation.setDuration(10000)
    animation.setStartValue(QtCore.QRect(0,0,0,0))
    animation.setEndValue(QtCore.QRect(0,0,200,200))
    animation.start()

    self.animation = animation

关于python - QPropertyAnimation 不适用于子部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6952852/

相关文章:

c++ - 如何通过指定的电子邮件客户端发送消息?

qt - 使 float 的 QDockWidget 无法获得焦点

jquery - 我可以使用 PNG 作为此水动画的 mask 而不是 <text> 标签吗?

css - 为什么这个 CSS3 动画没有运行?

python 队列、SimpleQueue、task_done

python - 滚动平均 pandas DataFrame 的所有值

python - Unicode(西里尔文)字符索引,用 python 重写

c++ - 由于线程问题,Qt Gui 未更新

animation - 如何制作带有动画 "close up"的 VBox ?

python - 如何在 Windows 64 上安装 NumPy?