Python Pyqt添加背景gif

时间:2017-01-18 00:31:28

标签: python pyqt pyqt4 pyqt5

我想在我的代码后面添加一个gif我似乎无法在这里工作是我的代码我正在工作

import sys
import webbrowser
import random
import time
import os
import subprocess
from PyQt4.QtCore import QSize, QTimer
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPainter, QPixmap, QMessageBox, QAction, QKeySequence, QFont, QFontMetrics, QMovie 
from PyQt4 import QtGui


class UIWindow(QWidget):
    def __init__(self, parent=None):
        super(UIWindow, self).__init__(parent)
        self.resize(QSize(600, 750))
        self.ToolsBTN = QPushButton('tab', self)
        self.ToolsBTN.resize(100,40)
        self.ToolsBTN.move(60, 300)

        self.CPS = QPushButton('tab1', self)
        self.CPS.resize(100,40)
        self.CPS.move(130,600)

        self.Creator = QPushButton('tab2', self)
        self.Creator.resize(100,40)
        self.Creator.move(260, 50)

class MainWindow(QMainWindow,):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(50, 50, 600, 750)
        self.setFixedSize(600, 750)
        self.startUIWindow()
    def startUIWindow(self):
        self.Window = UIWindow(self)
        self.setWindowTitle("My Program")
      self.show()

if __name__ == '__main__':
   app = QApplication(sys.argv)
   w = MainWindow()
   sys.exit(app.exec_())

这就是我现在所拥有的一切,我想让我的Dance.gif在后面玩,但如果有人可以帮我解决这个问题,那么它对我不起作用!

2 个答案:

答案 0 :(得分:2)

您必须使用QMovie重现电影,每次更改图像时,您必须使用当前像素图刷新背景。

__ init __函数中添加下一个代码:

self.movie = QMovie("{filename}.gif")
self.movie.frameChanged.connect(self.repaint)
self.movie.start()

我们实现了paintEvent函数:

def paintEvent(self, event):
    currentFrame = self.movie.currentPixmap()
    frameRect = currentFrame.rect()
    frameRect.moveCenter(self.rect().center())
    if frameRect.intersects(event.rect()):
        painter = QPainter(self)
        painter.drawPixmap(frameRect.left(), frameRect.top(), currentFrame)

完整代码:

import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QMovie, QPainter, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QWidget


class UIWindow(QWidget):
    def __init__(self, parent=None):
        super(UIWindow, self).__init__(parent)
        self.resize(QSize(600, 750))
        self.ToolsBTN = QPushButton('tab', self)
        self.ToolsBTN.resize(100, 40)
        self.ToolsBTN.move(60, 300)

        self.CPS = QPushButton('tab1', self)
        self.CPS.resize(100, 40)
        self.CPS.move(130, 600)

        self.Creator = QPushButton('tab2', self)
        self.Creator.resize(100, 40)
        self.Creator.move(260, 50)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(50, 50, 600, 750)
        self.setFixedSize(600, 750)
        self.startUIWindow()

        self.movie = QMovie("animated-dancing-image-0028.gif")
        self.movie.frameChanged.connect(self.repaint)
        self.movie.start()

    def startUIWindow(self):
        self.Window = UIWindow(self)
        self.setWindowTitle("My Program")
        self.show()

    def paintEvent(self, event):
        currentFrame = self.movie.currentPixmap()
        frameRect = currentFrame.rect()
        frameRect.moveCenter(self.rect().center())
        if frameRect.intersects(event.rect()):
            painter = QPainter(self)
            painter.drawPixmap(frameRect.left(), frameRect.top(), currentFrame)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())

GIF:

enter image description here

输出:

enter image description here

enter image description here

答案 1 :(得分:0)

我是在这个班上实现的...

class backgroundView(QGraphicsView):
    def __init__ (self,movie):
        super(backgroundView,self).__init__()
        self.movie=movie
        self.display_pixmap=movie.currentPixmap()
        self.setStyleSheet('QGraphicsView {background-color: rgb(0,0,0);}')

    def paintEvent(self,event):
        self.display_pixmap = self.movie.currentPixmap().scaled(self.my_size)
        painter=QPainter()
        painter.begin(self.viewport())
        painter.fillRect(event.rect(),self.palette().color(QPalette.Window))
        x = (self.width() - self.display_pixmap.width())/2
        y = (self.height() - self.display_pixmap.height())/2
        painter.drawPixmap(x, y, self.display_pixmap)
        painter.end()

    def resizeEvent(self, event):
        self.my_size=event.size()

然后主窗口类看起来像这样……

class MainWindow(QMainWindow):
    def __init__(self):
        view=backgroundView(self.movie)
        mainLayout=QVBoxLayout()
        self.setCentralWidget(view)
        view.setLayout(mainLayout)
        button=QPushButton('Button')
        mainLayout.addWidget(button)
        self.movie.frameChanged.connect(view.update)
        self.movie.start()
        self.show()

这将用GIF填充背景,并将其缩放到窗口大小。 我从我用来发现here的背景图像的类中改编了此内容。我不知道为什么,但是除非您设置视图的样式表,否则它不会重新绘制。