Python 在主窗口上使用QProcessAnimation
我在尝试设置QMainWindow的动画时遇到问题。我正在尝试为侧面板制作“幻灯片”动画。如果我在“app.exec”之前调用它,效果很好,但是调用“animate_out”函数似乎什么都不做。有什么想法吗 PS:您可以在底部取消对代码的注释,以查看我正在查找的示例 谢谢Python 在主窗口上使用QProcessAnimation,python,qt,pyqt,pyside,Python,Qt,Pyqt,Pyside,我在尝试设置QMainWindow的动画时遇到问题。我正在尝试为侧面板制作“幻灯片”动画。如果我在“app.exec”之前调用它,效果很好,但是调用“animate_out”函数似乎什么都不做。有什么想法吗 PS:您可以在底部取消对代码的注释,以查看我正在查找的示例 谢谢 # PYQT IMPORTS from PyQt4 import QtCore, QtGui import sys import UI_HUB # MAIN HUB CLASS class HUB(QtGui.QMainW
# PYQT IMPORTS
from PyQt4 import QtCore, QtGui
import sys
import UI_HUB
# MAIN HUB CLASS
class HUB(QtGui.QMainWindow, UI_HUB.Ui_HUB):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.setCentralWidget(self._Widget)
self.setWindowTitle('HUB - 0.0')
self._Widget.installEventFilter(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.set_size()
self.animate_out()
def set_size(self):
# Finds available and total screen resolution
resolution_availabe = QtGui.QDesktopWidget().availableGeometry()
ava_height = resolution_availabe.height()
self.resize(380, ava_height)
def animate_out(self):
animation = QtCore.QPropertyAnimation(self, "pos")
animation.setDuration(400)
animation.setStartValue(QtCore.QPoint(1920, 22))
animation.setEndValue(QtCore.QPoint(1541, 22))
animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
animation.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
form = HUB()
form.show()
form.raise_()
form.activateWindow()
# Doing the animation here works just fine
# animation = QtCore.QPropertyAnimation(form, "pos")
# animation.setDuration(400)
# animation.setStartValue(QtCore.QPoint(1920, 22))
# animation.setEndValue(QtCore.QPoint(1541, 22))
# animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
# animation.start()
app.exec_()
问题是
animation
对象不会超出animate\u out
out函数的范围。
要解决此问题,必须将动画
对象作为成员添加到HUB
类中
在我的示例代码中,我还将动画的创建和播放拆分为不同的函数
# [...] skipped
class HUB(QtGui.QMainWindow, UI_HUB.Ui_HUB):
def __init__(self):
# [...] skipped
self.create_animations() # see code below
self.animate_out()
def set_size(self):
# [...] skipped
def create_animations(self):
# set up the animation object
self.animation = QtCore.QPropertyAnimation(self, "pos")
self.animation.setDuration(400)
self.animation.setStartValue(QtCore.QPoint(1920, 22))
self.animation.setEndValue(QtCore.QPoint(1541, 22))
self.animation.setEasingCurve(QtCore.QEasingCurve.OutCubic)
def animate_out(self)
# use the animation object
self.animation.start()
# [...] skipped
您是否忘记在函数中调用
animation.start()
?您好@Mailerdaimon,谢谢您的回复。是的,我忘了在示例中添加“animation.start()”行广告。不过,将它添加到脚本中似乎没有什么不同。请尝试在构造函数之后启动动画。我不知道它在“showEvent”中是否有效,但你可以试试。哦,动画不是这个班的成员!调用animation.start()后,它将超出范围。尝试使用self.animation=QtCore[…]
@Mailerdaimon作为成员添加动画谢谢先生。添加“self.animation=animation”修复了该问题。