gpt4 book ai didi

python - Qt4 中的 PushButton 没有动画

转载 作者:行者123 更新时间:2023-12-01 05:27:17 26 4
gpt4 key购买 nike

我是这个论坛以及 Python 和 PyQt 的新手。在学习了一些基础知识之后,在尝试使用 Qt 学习动画时,我希望编写一个简单的应用程序,其中通过单击另一个按钮将按钮动画地从屏幕左侧带到某个位置。在这个过程中,我最终编写了这样的代码:

class AppMainUI(QtGui.QMainWindow, Ui_MainWindow):

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

self.setupUi(self)

self.setWindowFlags(QtCore.Qt.Window|QtCore.Qt.FramelessWindowHint)
self.connect(self.pushButton_4, QtCore.SIGNAL("clicked()"), self.onExitClicked)
self.connect(self.pushButton_5, QtCore.SIGNAL("clicked()"), self.animateButtons)

def animateButtons(self):
animation = QtCore.QPropertyAnimation(self.pushButton, "geometry")
animation.setDuration(2)
animation.setStartValue(QtCore.QRect(0, 0, self.pushButton.width(), self.pushButton.height()))
animation.setEndValue(QtCore.QRect(760, 280, self.pushButton.width(), self.pushButton.height()))
animation.setEasingCurve(QtCore.QEasingCurve.OutElastic)
animation.start()

def onExitClicked(self):
sys.exit(0)

App = QtGui.QApplication(sys.argv)
UI = AppMainUI()
UI.show()
App.exec_()

我看到按钮移动到位置 (0, 0),这实际上是我的动画开始位置。我在这里做错了什么吗?

最佳答案

问题是由于垃圾收集器造成的;当使用 animation.start() 语句完成 animateButtons(self) 方法时,animation 对象将被垃圾收集器销毁,因此动画不会发生。

只需在对象名称的开头添加 self 即可:

self.animation = QtCore.QPropertyAnimation(...)
self.animation.setStartValue(...)
...

还要将持续时间增加到至少 1000 毫秒,以便动画变得可见,否则它将执行得太快,以至于您看不到它。

关于python - Qt4 中的 PushButton 没有动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21074628/

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