2

我有一个小的 Python 脚本,它制作了一个透明窗口,用于在屏幕上显示图形,我想为该图形设置动画,但我完全不确定如何或从哪里开始。这是我至少拥有的:

import sys
from PyQt4 import QtGui, Qt, QtCore

class Transparent(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setAttribute(Qt.Qt.WA_NoSystemBackground)
        self.setAutoFillBackground(True)

        pixmap = QtGui.QPixmap("test1.gif")
                pixmap2 = QtGui.QPixmap("test2.gif")
        width = pixmap.width()
        height = pixmap.height()

        self.setWindowTitle("Status")
            self.resize(width, height)

            self.label = QtGui.QLabel(self)


        def animateEvent():
                    imgnumber = 0
                    try:
                        if imgnumber == 1:
                            self.label.setPixmap(QtGui.QPixmap("test1.gif"))
                            self.setMask(pixmap.mask())
                            imgnumber = 0
                        else:
                            self.label.setPixmap(QtGui.QPixmap("test2.gif"))
                            self.setMask(pixmap2.mask())
                            imgnumber = 1
                    finally:
                        QtCore.QTimer.singleShot(1000, animateEvent)

                animateEvent()


        def paintEvent(self,event):
        self.setAttribute(Qt.Qt.WA_NoSystemBackground)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    x = Transparent()
    x.show()
    app.exec_()

这感觉就像它具有正确的成分,但像素图没有更新。

我尝试了 QMovie,但随后应该透明的窗口区域被黑色填充。

4

3 回答 3

0

First, just make sure your animated gif really does have a proper transparent background. The following code works for me, using this fire image as a source:

class Transparent(QtGui.QWidget):

    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        filename = "test.gif"
        
        size = QtGui.QImage(filename).size()

        self.setWindowTitle("Status")

        layout = QtGui.QVBoxLayout(self)
        layout.setMargin(0)

        self.movie = QtGui.QMovie(filename)
        self.label = QtGui.QLabel(self)
        self.label.setMovie(self.movie)

        layout.addWidget(self.label)
        self.resize(size)

        self.movie.start()

This will create a completely transparent and frameless window, with the animated gif playing in a QMovie. There is no black being drawn behind the image. It should fully see through to what ever is underneath.

It is not so far off from your original code. You shouldn't need to set the mask, or do a paint event.

于 2012-12-09T00:21:47.637 回答
0

check out this code from www.daniweb.com and see if you can modify it to your needs:

import sys 
from PyQt4.QtCore import *
from PyQt4.QtGui import * 

class MoviePlayer(QWidget): 
    def __init__(self, gif, parent=None): 
        super(MoviePlayer, self).__init__(parent)

        self.setGeometry(200, 200, 400, 400)
        self.setWindowTitle("QMovie to show animated gif")

        self.movie_screen = QLabel()
        self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)        
        self.movie_screen.setAlignment(Qt.AlignCenter) 

        btn_start = QPushButton("Start Animation")
        btn_start.clicked.connect(self.start) 

        btn_stop = QPushButton("Stop Animation")
        btn_stop.clicked.connect(self.stop) 

        main_layout = QVBoxLayout() 
        main_layout.addWidget(self.movie_screen)
        main_layout.addWidget(btn_start) 
        main_layout.addWidget(btn_stop)
        self.setLayout(main_layout) 

        self.movie = QMovie(gif, QByteArray(), self) 
        self.movie.setCacheMode(QMovie.CacheAll) 
        self.movie.setSpeed(100) 
        self.movie_screen.setMovie(self.movie) 


    def start(self):
        """
        Start animation
        """
        self.movie.start()

    def stop(self):
        """
        Stop the animation
        """
        self.movie.stop()


app = QApplication(sys.argv) 
player = MoviePlayer("/path/to/image.gif") 
player.show() 
sys.exit(app.exec_())
于 2012-12-09T00:22:12.443 回答
0

这最终只是对疏忽的简单纠正。

imgnumber需要在 def as 之外,并且每次更改时都self.imgnumber需要命名。self.imgnumber

于 2012-12-09T16:16:32.720 回答