gpt4 book ai didi

python - QPropertyAnimation 不适用于子部件

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:12 25 4
gpt4 key购买 nike

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

我正在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com