[PyQt] setWindowOpacity
David Boddie
dboddie at trolltech.com
Wed Nov 11 19:00:07 GMT 2009
On Wed Nov 11 17:32:36 GMT 2009, Tim and Alison Bentley wrote:
> What I would like to do is to be able to fade the images in and out
>
> self.setWindowOpacity(0.5)
> self.display.setPixmap(image)
> self.setWindowOpacity(1)
>
> I accept it may be necessary to add delays in but even with 1 second delays
> in the code the screen does not fade and reappear. If I remove the 3rd
> line the display does go semi transparent.
This won't work as you expect because of the way control flow works for
event-based frameworks like PyQt. Changes to the user interface typically
take place when control returns to the framework, and not at the time you
call a method on a widget.
If you insert a delay in the above code, it just takes longer for control
to return to the framework - the changes you made to the label are then
applied, and the first change to its opacity may even be discarded if not
immediately overridden by the second change.
What you need to do is to change the opacity, then tell the framework that
you want to update the opacity again later. You can do this by creating a
single-shot timer and another method:
# [In some other method...]
self.setWindowOpacity(0.5)
self.display.setPixmap(image)
QTimer.singleShot(1000, self.updateOpacity)
def updateOpacity(self):
self.setWindowOpacity(1)
There are other ways to control the timing of this kind of animation, but
this is possibly the simplest for your use case. Here's some example code to
play with:
http://www.diotavelli.net/PyQtWiki/Fading%20and%20unfading%20a%20widget%20with%20a%20delay
David
More information about the PyQt
mailing list