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