PLAY animated GIF files with PySide

vegaseat 3 Tallied Votes 3K Views Share

Use the PySide (PyQT) GUI toolkit to play animated gif files.

'''ps_AniGifPlay2.py
use PySide (public PyQt) to play an animated gif file

depending on your version of Python
you can use the Windows self-extracting installer
PySide-1.2.1.win32-py2.7.exe
PySide-1.2.1.win32-py3.3.exe
from:
http://www.lfd.uci.edu/~gohlke/pythonlibs/

PySide is the official LGPL-licensed version of PyQT
tested with Python27 and Python33  by  vegaseat  11apr2014
'''

import sys 

from PySide.QtCore import *
from PySide.QtGui import * 

class MoviePlayer(QWidget): 
    def __init__(self, parent=None): 
        QWidget.__init__(self, parent) 
        # setGeometry(x_pos, y_pos, width, height)
        self.setGeometry(200, 200, 400, 400)
        self.setWindowTitle("QMovie to show animated gif")
        
        # set up the movie screen on a label
        self.movie_screen = QLabel()
        # expand and center the label 
        self.movie_screen.setSizePolicy(QSizePolicy.Expanding, 
            QSizePolicy.Expanding)        
        self.movie_screen.setAlignment(Qt.AlignCenter) 

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

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

        # positin the widgets
        main_layout = QVBoxLayout() 
        main_layout.addWidget(self.movie_screen)
        main_layout.addWidget(self.btn_start) 
        main_layout.addWidget(self.btn_stop)
        self.setLayout(main_layout) 
                
        # use an animated gif file you have in the working folder
        # or give the full file path
        ag_file = "AG_Dog.gif"
        self.movie = QMovie(ag_file, QByteArray(), self) 
        self.movie.setCacheMode(QMovie.CacheAll) 
        self.movie.setSpeed(100) 
        self.movie_screen.setMovie(self.movie) 
        # optionally display first frame
        self.movie.start()
        self.movie.stop()
        
    def start(self):
        """sart animnation"""
        self.movie.start()
        
    def stop(self):
        """stop the animation"""
        self.movie.stop()


# you can replace [] with sys.argv commandline arg 
app = QApplication([]) 
player = MoviePlayer() 
player.show() 
app.exec_()
Lardmeister 461 Posting Virtuoso

To install the PySide package on Linux use
for Python2 -->
sudo apt-get install python-pyside
or for Python3 -->
sudo apt-get install python3-pyside

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.