如何使用python PyQt5在gui中放置gif

时间:2019-03-11 14:57:45

标签: python pyqt5

该程序是一个trafficlight程序,但是我想在窗口的右侧空间放置一个gif,当颜色为绿色时将显示行走的人gif,而在红色或黄色时则显示停止的gif,所以我尝试使用QMovie我得到的结果好坏参半,但仍然出现错误,或者gif不会出现在窗口中,您能帮我吗?

from itertools import cycle
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer,Qt,QPoint
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.QtGui import QPainter,QColor,QMovie
class TrafficLight(QMainWindow):
    def __init__(self,parent = None):
        super(TrafficLight, self).__init__(parent)
        self.setWindowTitle("TrafficLight ")
        self.traffic_light_color1 = cycle(\[
            QColor('red'),
            QColor('gray'),
            QColor('gray')
        \])
        self.traffic_light_color2 = cycle(\[
            QColor('gray'),
            QColor('yellow'),
            QColor('gray')
        \])
        self.traffic_light_color3 = cycle(\[
            QColor('gray'),
            QColor('gray'),
            QColor('green')
        \])

        self._current_color1 = next(self.traffic_light_color1)
        self._current_color2 = next(self.traffic_light_color2)
        self._current_color3 = next(self.traffic_light_color3)
        timer = QTimer(self, timeout=self.change_color)
        x = 0
        if x == 0 :
            self.movie1 = QMovie("Walking-man2[enter image description here][1].gif")
            self.movie1.frameChanged.connect(self.repaint)
            self.movie1.start()
            timer.start(30*100)
            x = 1
        elif x == 1 :
            self.movie1 = QMovie("tenor(1).gif")
            self.movie1.frameChanged.connect(self.repaint)
            self.movie1.start()
            timer.start(10*100)
            x = 2
        elif x == 2:
            self.movie1 = QMovie("tenor(1).gif")
            self.movie1.frameChanged.connect(self.repaint)
            self.movie1.start()
            timer.start(40*100)
            x = 0
        self.resize(700, 510)

    @QtCore.pyqtSlot()
    def change_color(self):
        self._current_color1 = next(self.traffic_light_color1)
        self._current_color2 = next(self.traffic_light_color2)
        self._current_color3 = next(self.traffic_light_color3)
        self.update()

    def paintEvent(self, event):
        p1 = QPainter(self)
        p1.setBrush(self._current_color1)
        p1.setPen(Qt.black)
        p1.drawEllipse(QPoint(125, 125), 50, 50)

        p2 = QPainter(self)
        p2.setBrush(self._current_color2)
        p2.setPen(Qt.black)
        p2.drawEllipse(QPoint(125, 250),50,50)

        p3 = QPainter(self)
        p3.setBrush(self._current_color3)
        p3.setPen(Qt.black)
        p3.drawEllipse(QPoint(125, 375),50,50)

        currentFrame = self.movie1.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__":
    import sys
    app = QApplication(sys.argv)
    w = TrafficLight()
    w.show() 
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

尽管答案并不像eyllanesc的那么花哨。您可以使椭圆依赖于可变颜色,然后更改存储的可变颜色并调用update()。 gif可以使用标签显示

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer,Qt,QPoint
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.QtGui import QPainter,QColor,QMovie

class TrafficLight(QtWidgets.QWidget):
    def __init__(self,parent = None):
        super(TrafficLight, self).__init__(parent)
        self.setWindowTitle("TrafficLight ")
        layout = QtWidgets.QHBoxLayout()
        self.setLayout(layout)
        self.lblGif = QtWidgets.QLabel()
        layout.addSpacing(300)
        layout.addWidget(self.lblGif)

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.changeLight)
        self.timer.start(3000)

        self.resize(700, 510)
        self.x = 0
        self.changeLight()

    def changeLight(self):
        if self.x == 0 :
            self.color1 = QColor('red')
            self.color2 = QColor('grey')
            self.color3 = QColor('grey')
            self.loadGif('wait.gif')
            self.x = 1

        elif self.x == 1 :
            self.color1 = QColor('grey')
            self.color2 = QColor('yellow')
            self.color3 = QColor('grey')
            self.loadGif('almost.gif')
            self.x = 2

        elif self.x == 2:
            self.color1 = QColor('grey')
            self.color2 = QColor('grey')
            self.color3 = QColor('green')
            self.loadGif('walk.gif')
            self.x = 0
        self.update()

    def loadGif(self, path):
        movie = QtGui.QMovie(path)
        self.lblGif.setMovie(movie)
        movie.start()

    def paintEvent(self, event):
        p1 = QPainter(self)
        p1.setBrush(self.color1)
        p1.setPen(Qt.black)
        p1.drawEllipse(QPoint(125, 125), 50, 50)
        p1.end()

        p2 = QPainter(self)
        p2.setBrush(self.color2)
        p2.setPen(Qt.black)
        p2.drawEllipse(QPoint(125, 250),50,50)
        p2.end()

        p3 = QPainter(self)
        p3.setBrush(self.color3)
        p3.setPen(Qt.black)
        p3.drawEllipse(QPoint(125, 375),50,50)
        p3.end()

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = TrafficLight()
    w.show()
    sys.exit(app.exec_())