Animation in pyqt5
-
I made a Farmer John test app to check out the visual performance of a fading dialog (tried it in wxpython -abysmal!)
#cython: language_level=3 import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog from PyQt5.QtCore import QPropertyAnimation, QEasingCurve app=QApplication(sys.argv) window=QMainWindow() window.setWindowTitle ('Test...') animation=QPropertyAnimation(window, bytes('windowOpacity', 'utf8')) animation.setDuration (5000) animation.setStartValue (1.0) animation.setEndValue (0.0) animation.finished.connect (window.close) animation.start () window.show () app.exec ()
I see it lurch a bit; it is by NO MEANS the smooth transition I have seen with other apps! I tried it as cython, wondering if a slow interpreter might be the culprit; nope. It fades a little; pauses for a couple of seconds; fades a teeny bit more; then finally disappears. Not the gradual transition one would expect.
Is my code wrong? What's up with this effect??
-
@Steve-REDACTED
I tried your code under Linux. What I see is what I presume you do not like: visually I see maybe 10 "steps" of animation during the 5 seconds. I think you want to see more "steps" than this so that it is "smoother", right?First immediately above
app.exec()
I put inanimation.valueChanged.connect(lambda value: print(value))
This showed me that that there are lots of calls to change the opacity value during the animation. Good.
Then I replaced that by:
animation.valueChanged.connect(lambda value: window.update())
I think you will be happier with this! I get lots of updates, much smoother.
Clearly the original problem is that the window does not get updated/reshown to the user frequently enough. Now it calls an explicit
update()
on every value update. I am not sure what the requirements are, how often the animation updates without you callingupdate()
yourself, but clearly in this case it is not enough without you giving it a helping hand. -
-
Thanks a zillion; I don't get why many of the online examples omitted that. A window that fades out is not an uncommon thing!
-
-
There's just one little frustrating quirk now: after the window fades out, as QT exits the window reappears for an instant, then disappears. I figured this made some sense since it was a return to state of the window's default opacity of 1; so I did a window.setWindowOpacity (0.0) [right after creating it with window=QMainWindow()] Unfortunately it still does it.
Going to do some searches now to see if anyone else worked around this...
-
Got the window flashing removed when the app exits. I changed
animation.finished.connect (window.close)
to call a function of mine that does the window.close (). When I added a time.sleep (1) after the window.close, it stops that behavior. So it appears qt needs more time when it is cleaning up the window, and when you prematurely interrupt it (with an application exit...), the window repaints for an instant.
AND: you don't need to set an initial opacity of 0...